﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using System.Data.SqlClient;
using XJLSC.QM;
using SMALLERP.PM;
using System.IO;
using FastReport;
using FastReport.Utils;
using SMALLERP;
using RestSharp;
using XJLSC.ComClass;
using Newtonsoft.Json;
using MSword = Microsoft.Office.Interop.Excel;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

namespace XJLSC.PM
{
    public partial class GXBGD : Form
    {
        // 创建一个 DataBase 类的实例
        DataBase db = new DataBase();
        private SqlDataReader sdr1;
        // DDKC 类的实例
        DDKC f;
        DDKC g;
        DDKC z;
        // GXBGDItem 类的实例
        GXBGDItem j;
        // 用于在内存中表示数据库表的 DataTable
        DataTable dt;
        // 另一个 GXBGDItem 类的实例
        GXBGDItem form1;
        // Report 类的私有实例
        private Report FReport;

        // DataSet 类的私有实例，用于在内存中缓存来自数据库的数据
        private DataSet FDataSet;
        private DataSet FDataSet1;
        // 存储报告的文件夹路径
        private string FReportsFolder;
        // 标志，指示报告当前是否在运行
        private bool FReportRunning;




        public GXBGD()
        {
            InitializeComponent();
        }
        //新构造函数
        public GXBGD(GXBGDItem form1)
            : this()
        {
            this.form1 = form1;
        }

        public delegate void CheckBoxClickedHandler(bool state);
        // DataGridViewCheckBoxHeaderCellEventArgs 类继承自 EventArgs 类
        public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
        {
            // 构造函数，接收一个布尔参数表示是否选中
            bool _bChecked;
            public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked)
            {
                _bChecked = bChecked;
            }
            // 属性，获取是否选中的状态
            public bool Checked
            {
                get { return _bChecked; }
            }
        }

        class DatagridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell
        {
            Point checkBoxLocation;
            Size checkBoxSize;
            bool _checked = false;
            Point _cellLocation = new Point();
            System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
                System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;

            // CheckBox 点击事件
            public event CheckBoxClickedHandler OnCheckBoxClicked;

            public DatagridViewCheckBoxHeaderCell()
            {
            }

            //protected override void Paint(System.Drawing.Graphics graphics,
            //    System.Drawing.Rectangle clipBounds,
            //    System.Drawing.Rectangle cellBounds,
            //    int rowIndex,
            //    DataGridViewElementStates dataGridViewElementState,
            //    object value,
            //    object formattedValue,
            //    string errorText,
            //    DataGridViewCellStyle cellStyle,
            //    DataGridViewAdvancedBorderStyle advancedBorderStyle,
            //    DataGridViewPaintParts paintParts)
            //{
            //    base.Paint(graphics, clipBounds, cellBounds, rowIndex,
            //        dataGridViewElementState, value,
            //        formattedValue, errorText, cellStyle,
            //        advancedBorderStyle, paintParts);
            //    Point p = new Point();
            //    Size s = CheckBoxRenderer.GetGlyphSize(graphics,
            //    System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
            //    p.X = cellBounds.Location.X +
            //        (cellBounds.Width / 2) - (s.Width / 2);
            //    p.Y = cellBounds.Location.Y +
            //        (cellBounds.Height / 2) - (s.Height / 2);
            //    _cellLocation = cellBounds.Location;
            //    checkBoxLocation = p;
            //    checkBoxSize = s;
            //    if (_checked)
            //        _cbState = System.Windows.Forms.VisualStyles.
            //            CheckBoxState.CheckedNormal;
            //    else
            //        _cbState = System.Windows.Forms.VisualStyles.
            //            CheckBoxState.UncheckedNormal;
            //    CheckBoxRenderer.DrawCheckBox
            //    (graphics, checkBoxLocation, _cbState);
            //}

            //protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
            //{
            //    Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
            //    if (p.X >= checkBoxLocation.X && p.X <=
            //        checkBoxLocation.X + checkBoxSize.Width
            //    && p.Y >= checkBoxLocation.Y && p.Y <=
            //        checkBoxLocation.Y + checkBoxSize.Height)
            //    {
            //        _checked = !_checked;
            //        if (OnCheckBoxClicked != null)
            //        {
            //            OnCheckBoxClicked(_checked);
            //            this.DataGridView.InvalidateCell(this);
            //        }

            //    }
            //    base.OnMouseClick(e);
            //}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strSql = "";
            DataSet ds = new DataSet();
            DataSet ds_scgjb = new DataSet();//主表
            DataSet ds_jjmx = new DataSet();//从表
            if (textBox1.Text == "1008" || textBox1.Text == "1007")
            {
                if (textBox2.Text == "" && textBox5.Text == "")
                {
                    strSql = "SELECT TA001 单别,TA002 单号,RTRIM(TA003) 下单日期,MB001 品号,MB002 品名,MB003 规格,ISNULL(MF003,'') 工序号,ISNULL(C.MW002,'') 工艺名称,ISNULL(MF008,'') 工艺说明,CEILING(TA015) 数量,CEILING(TA017) 已入库数量,TA083 项目号,ISNULL(TY003,'') 排产日期,RTRIM(TA021) 工作中心 FROM MOCTA LEFT JOIN COPTC ON TA026=TC001 AND TA027=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN CMSMW C ON C.MW001=MF004 LEFT JOIN MOCTY ON TY001=TA001 AND TY002=TA002 AND ISNULL(MF003,'****')=TY019 WHERE (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (MOCTA.TA034 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='') AND TA030='1' AND MOCTA.TA083 LIKE '%" + textBox3.Text + "%' AND MOCTA.TA002 LIKE '%" + textBox5.Text + "%' AND MOCTA.TA011<>'y' AND MOCTA.TA013='Y'";
                }
                else
                {
                    if (checkBox3.CheckState == CheckState.Checked)
                    {
                        strSql = "SELECT TA001 单别,TA002 单号,RTRIM(TA003) 下单日期,MB001 品号,MB002 品名,MB003 规格,ISNULL(MF003,'') 工序号,ISNULL(C.MW002,'') 工艺名称,ISNULL(MF008,'') 工艺说明,CEILING(TA015) 数量,CEILING(TA017) 已入库数量,TA083 项目号,ISNULL(TY003,'') 排产日期,RTRIM(TA021) 工作中心 FROM MOCTA LEFT JOIN COPTC ON TA026=TC001 AND TA027=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN CMSMW C ON C.MW001=MF004 LEFT JOIN MOCTY ON TY001=TA001 AND TY002=TA002 AND ISNULL(MF003,'****')=TY019 WHERE (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (MOCTA.TA034 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='') AND TA030='1' AND MOCTA.TA083 LIKE '%" + textBox3.Text + "%' AND MOCTA.TA002 LIKE '%" + textBox5.Text + "%' AND MOCTA.TA011<>'y' AND MOCTA.TA013='Y'";
                    }
                    else
                    {
                        strSql = "SELECT TA001 单别,TA002 单号,RTRIM(TA003) 下单日期,MB001 品号,MB002 品名,MB003 规格,ISNULL(MF003,'') 工序号,ISNULL(C.MW002,'') 工艺名称,ISNULL(MF008,'') 工艺说明,CEILING(TA015) 数量,CEILING(TA017) 已入库数量,TA083 项目号,ISNULL(TY003,'') 排产日期,RTRIM(TA021) 工作中心 FROM MOCTA LEFT JOIN COPTC ON TA026=TC001 AND TA027=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN CMSMW C ON C.MW001=MF004 LEFT JOIN MOCTY ON TY001=TA001 AND TY002=TA002 AND ISNULL(MF003,'****')=TY019 WHERE (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (MOCTA.TA034 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='') AND TA030='1' AND MOCTA.TA083 LIKE '%" + textBox3.Text + "%' AND MOCTA.TA002 LIKE '%" + textBox5.Text + "%' AND MOCTA.TA011<>'y' AND MOCTA.TA013='Y'";
                    }
                }
            }
            else
            {
                if (textBox2.Text == "" && textBox5.Text == "")
                {
                    strSql = "SELECT TA001 单别,TA002 单号,RTRIM(TA003) 下单日期,MB001 品号,MB002 品名,MB003 规格,'' 工序号,'' 工艺名称,'' 工艺说明,CEILING(TA015) 数量,CEILING(TA017) 已入库数量,TA083 项目号,ISNULL(TY003,'') 排产日期,RTRIM(TA021) 工作中心 FROM MOCTA LEFT JOIN COPTC ON TA026=TC001 AND TA027=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN MOCTY ON TY001=TA001 AND TY002=TA002 AND '****'=TY019 WHERE (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (MOCTA.TA034 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='') AND TA030='1' AND MOCTA.TA083 LIKE '%" + textBox3.Text + "%' AND MOCTA.TA002 LIKE '%" + textBox5.Text + "%' AND MOCTA.TA011<>'y' AND MOCTA.TA013='Y'";
                }
                else
                {
                    if (checkBox3.CheckState == CheckState.Checked)
                    {
                        strSql = "SELECT TA001 单别,TA002 单号,RTRIM(TA003) 下单日期,MB001 品号,MB002 品名,MB003 规格,'' 工序号,'' 工艺名称,'' 工艺说明,CEILING(TA015) 数量,CEILING(TA017) 已入库数量,TA083 项目号,ISNULL(TY003,'') 排产日期,RTRIM(TA021) 工作中心 FROM MOCTA LEFT JOIN COPTC ON TA026=TC001 AND TA027=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN MOCTY ON TY001=TA001 AND TY002=TA002 AND '****'=TY019 WHERE (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (MOCTA.TA034 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='') AND TA030='1' AND MOCTA.TA083 LIKE '%" + textBox3.Text + "%' AND MOCTA.TA002 LIKE '%" + textBox5.Text + "%' AND MOCTA.TA011<>'y' AND MOCTA.TA013='Y'";
                    }
                    else
                    {
                        strSql = "SELECT TA001 单别,TA002 单号,RTRIM(TA003) 下单日期,MB001 品号,MB002 品名,MB003 规格,'' 工序号,'' 工艺名称,'' 工艺说明,CEILING(TA015) 数量,CEILING(TA017) 已入库数量,TA083 项目号,ISNULL(TY003,'') 排产日期,RTRIM(TA021) 工作中心 FROM MOCTA LEFT JOIN COPTC ON TA026=TC001 AND TA027=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN MOCTY ON TY001=TA001 AND TY002=TA002 AND '****'=TY019 WHERE (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (MOCTA.TA034 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='') AND TA030='1' AND MOCTA.TA083 LIKE '%" + textBox3.Text + "%' AND MOCTA.TA002 LIKE '%" + textBox5.Text + "%' AND MOCTA.TA011<>'y' AND MOCTA.TA013='Y'";
                    }
                }
            }
            //MessageBox.Show(strSql, "系统提示");
            //textBox12.Text = strSql;
            //return;
            try
            {
                DataTable dt;

                dt = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];
                gridControl1.DataSource = dt;
                //SqlCommand comm2 = new SqlCommand(strSql, db.Conn1);
                //SqlDataAdapter da2 = new SqlDataAdapter(comm2);
                //da2.Fill(ds, "emp");
                ////ds_scgjb = db.GetDataSet1(strSql, "MOCTA");
                 

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }

            //if (checkBox1.CheckState == CheckState.Checked)
            //{
            //    strSql = "SELECT RIGHT('0000'+RTRIM(CAST(ROW_NUMBER()OVER(ORDER BY MF003,MG003) AS CHAR(4))),4),B.TA001,B.TA002,MF003,ISNULL(MG006,'') MG006,TA034,C.MW002,MF008 AS MG007,CAST(TA015*(1+INVMB.MB402) AS REAL) TA015,ISNULL(CEILING(YPSL),0),CAST(TA015*(1+INVMB.MB402)-ISNULL(CEILING(YPSL),0) AS REAL) WBSL,0 HGSL,0 BLSL,0 GFSL,0 LFSL,'' YGBH,'' YGXM,'' ZRGX,'' BZ,TA001 AS TE001,TA002 AS TE002,CAST(ISNULL(MGD09,0) AS REAL) AS JJDJ,ISNULL(MF023,'') 下阶品号,ISNULL(F.MB002,'') 下阶品名,ISNULL(F.MB029,'') 下阶图号 FROM MOCTA B LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN BOMMG ON MF001=MG001 AND MF002=MG002 AND MF003=MG003 LEFT JOIN CMSMW C ON C.MW001=MF004 LEFT JOIN CMSMW D ON D.MW001=MGD01 LEFT JOIN V_SFCTE ON B.TA001=TE006 AND B.TA002=TE007 AND MF003=TE008 LEFT JOIN ITEM E ON E.ITEM_CODE=MF023 LEFT JOIN INVMB F ON MF023=F.MB001 WHERE (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (B.TA034 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='') AND TA030='1' AND B.TA083 LIKE '%" + textBox3.Text + "%' AND B.TA002 LIKE '%" + textBox5.Text + "%' AND ISNULL(D.MW002,'') LIKE '%" + textBox2.Text + "%' ORDER BY MF003,MG006";
            //}
            //else
            //{
            //    strSql = "SELECT RIGHT('0000'+RTRIM(CAST(ROW_NUMBER()OVER(ORDER BY MF003,MG003) AS CHAR(4))),4),B.TA001,B.TA002,MF003,ISNULL(MG006,'') MG006,TA034,C.MW002,MF008 AS MG007,CAST(TA015*(1+INVMB.MB402) AS REAL) TA015,ISNULL(CEILING(YBSL),0),CAST(TA015*(1+INVMB.MB402)-ISNULL(CEILING(YBSL),0) AS REAL) WBSL,0 HGSL,0 BLSL,0 GFSL,0 LFSL,'' YGBH,'' YGXM,'' ZRGX,'' BZ,TA001 AS TE001,TA002 AS TE002,CAST(ISNULL(MGD09,0) AS REAL) AS JJDJ,ISNULL(MF023,'') 下阶品号,ISNULL(F.MB002,'') 下阶品名,ISNULL(F.MB029,'') 下阶图号 FROM MOCTA B LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN BOMMG ON MF001=MG001 AND MF002=MG002 AND MF003=MG003 LEFT JOIN CMSMW C ON C.MW001=MF004 LEFT JOIN CMSMW D ON D.MW001=MGD01 LEFT JOIN V_SFCTE ON B.TA001=TE006 AND B.TA002=TE007 AND MF003=TE008 LEFT JOIN ITEM E ON E.ITEM_CODE=MF023 LEFT JOIN INVMB F ON MF023=F.MB001 WHERE (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (B.TA034 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='') AND TA030='1' AND B.TA083 LIKE '%" + textBox3.Text + "%' AND B.TA002 LIKE '%" + textBox5.Text + "%' AND ISNULL(D.MW002,'') LIKE '%" + textBox2.Text + "%' ORDER BY MF003,MG006";
            //}

            //try
            //{
            //    SqlCommand comm3 = new SqlCommand(strSql, db.Conn1);
            //    SqlDataAdapter da3 = new SqlDataAdapter(comm3);
            //    da3.Fill(ds, "job");
            //    //ds_jjmx = db.GetDataSet1(strSql, "SFCTA");
            //}
            //catch
            //{

            //}
            //gridControl1.DataSource = ds;
            //this.treeListLookUpEdit1TreeList.DataSource = ds;
            //ds.Tables.Add(ds_scgjb.Tables[0].Copy());
            //ds.Tables.Add(ds_jjmx.Tables[0].Copy());

            ////主从字表约束字段：这里有三个约束字段       
            //DataColumn[] column3 = new DataColumn[] {
            //  ds.Tables["emp"].Columns["单别"], ds.Tables["emp"].Columns["单号"]};
            //DataColumn[] column4 = new DataColumn[] {
            //  ds.Tables["job"].Columns["TA001"], ds.Tables["job"].Columns["TA002"]};

            ////建立约束名
            //if (ds.Relations.IndexOf("jjgz") < 0)
            //{
            //    ds.Relations.Add("jjgz", column3, column4);
            //}
            ////gridControl1.DataSource = ds.Tables[0];
            //this.gridControl1.DataSource = ds.Tables["emp"];

            //以下执行单行选择的代码
            try
            {

                this.dataGridView2.Rows.Clear();
                if (textBox1.Text == "1008" || textBox1.Text == "1007")
                {
                    if (checkBox1.CheckState == CheckState.Checked)
                    {
                        strSql = "SELECT RIGHT('0000'+RTRIM(CAST(ROW_NUMBER()OVER(ORDER BY MF003,MG003) AS CHAR(4))),4),B.TA001,B.TA002,MF003,ISNULL(MG006,'') MG006,TA034,C.MW002,MF008 AS MG007,CAST(TA015*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402) AS REAL) TA015,ISNULL(CEILING(YPSL),0),CAST(TA015*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402)-ISNULL(CEILING(YPSL),0) AS REAL) WBSL,0 HGSL,0 BLSL,0 GFSL,0 LFSL,'' YGBH,'' YGXM,'' ZRGX,'' BZ,TA001 AS TE001,TA002 AS TE002,CAST(ISNULL(MGD09,0) AS REAL) AS JJDJ,ISNULL(MF023,'') 下阶品号,ISNULL(F.MB002,'') 下阶品名,ISNULL(F.MB029,'') 下阶图号,CONVERT(CHAR(10),GETDATE(),111) 要求完工日期 FROM MOCTA B LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN BOMMG ON MF001=MG001 AND MF002=MG002 AND MF003=MG003 LEFT JOIN CMSMW C ON C.MW001=MF004 LEFT JOIN CMSMW D ON D.MW001=MGD01 LEFT JOIN V_SFCTE ON B.TA001=TE006 AND B.TA002=TE007 AND MF003=TE008 LEFT JOIN ITEM E ON E.ITEM_CODE=MF023 LEFT JOIN INVMB F ON MF023=F.MB001 WHERE  B.TA001='" + gridView1.GetFocusedRowCellValue("单别").ToString() + "' AND B.TA002='" + gridView1.GetFocusedRowCellValue("单号").ToString() + "' AND ISNULL(D.MW002,'') LIKE '%" + textBox2.Text + "%' ORDER BY MF003,MG006";
                    }
                    else
                    {
                        strSql = "SELECT RIGHT('0000'+RTRIM(CAST(ROW_NUMBER()OVER(ORDER BY MF003,MG003) AS CHAR(4))),4),B.TA001,B.TA002,MF003,ISNULL(MG006,'') MG006,TA034,C.MW002,MF008 AS MG007,CAST(TA015*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402) AS REAL) TA015,ISNULL(CEILING(YBSL),0),CAST(TA015*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402)-ISNULL(CEILING(YBSL),0) AS REAL) WBSL,0 HGSL,0 BLSL,0 GFSL,0 LFSL,'' YGBH,'' YGXM,'' ZRGX,'' BZ,TA001 AS TE001,TA002 AS TE002,CAST(ISNULL(MGD09,0) AS REAL) AS JJDJ,ISNULL(MF023,'') 下阶品号,ISNULL(F.MB002,'') 下阶品名,ISNULL(F.MB029,'') 下阶图号,CONVERT(CHAR(10),GETDATE(),111) 要求完工日期 FROM MOCTA B LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN BOMMG ON MF001=MG001 AND MF002=MG002 AND MF003=MG003 LEFT JOIN CMSMW C ON C.MW001=MF004 LEFT JOIN CMSMW D ON D.MW001=MGD01 LEFT JOIN V_SFCTE ON B.TA001=TE006 AND B.TA002=TE007 AND MF003=TE008 LEFT JOIN ITEM E ON E.ITEM_CODE=MF023 LEFT JOIN INVMB F ON MF023=F.MB001 WHERE  B.TA001='" + gridView1.GetFocusedRowCellValue("单别").ToString() + "' AND B.TA002='" + gridView1.GetFocusedRowCellValue("单号").ToString() + "' AND ISNULL(D.MW002,'') LIKE '%" + textBox2.Text + "%' ORDER BY MF003,MG006";
                    }
                }
                else
                {
                    if (checkBox1.CheckState == CheckState.Checked)
                    {
                        strSql = "SELECT RIGHT('0000'+RTRIM(CAST(ROW_NUMBER()OVER(ORDER BY MF003,MG003) AS CHAR(4))),4),B.TA001,B.TA002, MF003,ISNULL(MG006,'') MG006,TA034,C.MW002,MF008 AS MG007,CAST(TA015*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402) AS REAL) TA015,ISNULL(CEILING(YPSL),0),CAST(TA015*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402)-ISNULL(CEILING(YPSL),0) AS REAL) WBSL,0 HGSL,0 BLSL,0 GFSL,0 LFSL,'' YGBH,'' YGXM,'' ZRGX,'' BZ,TA001 AS TE001,TA002 AS TE002,CAST(ISNULL(MGD09,0) AS REAL) AS JJDJ,ISNULL(MF023,'') 下阶品号,ISNULL(F.MB002,'') 下阶品名,ISNULL(F.MB029,'') 下阶图号,CONVERT(CHAR(10),GETDATE(),111) 要求完工日期 FROM MOCTA B LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN BOMMG ON MF001=MG001 AND MF002=MG002 AND MF003=MG003 LEFT JOIN CMSMW C ON C.MW001=MF004 LEFT JOIN CMSMW D ON D.MW001=MGD01 LEFT JOIN V_SFCTE ON B.TA001=TE006 AND B.TA002=TE007 AND MF003=TE008 LEFT JOIN ITEM E ON E.ITEM_CODE=MF023 LEFT JOIN INVMB F ON MF023=F.MB001 WHERE  B.TA001='" + gridView1.GetFocusedRowCellValue("单别").ToString() + "' AND B.TA002='" + gridView1.GetFocusedRowCellValue("单号").ToString() + "' AND ISNULL(D.MW002,'') LIKE '%" + textBox2.Text + "%'";
                    }
                    else
                    {
                        strSql = "SELECT RIGHT('0000'+RTRIM(CAST(ROW_NUMBER()OVER(ORDER BY MF003,MG003) AS CHAR(4))),4),B.TA001,B.TA002, MF003,ISNULL(MG006,'') MG006,TA034,C.MW002,MF008 AS MG007,CAST(TA015*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402) AS REAL) TA015,ISNULL(CEILING(YBSL),0),CAST(TA015*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402)-ISNULL(CEILING(YBSL),0) AS REAL) WBSL,0 HGSL,0 BLSL,0 GFSL,0 LFSL,'' YGBH,'' YGXM,'' ZRGX,'' BZ,TA001 AS TE001,TA002 AS TE002,CAST(ISNULL(MGD09,0) AS REAL) AS JJDJ,ISNULL(MF023,'') 下阶品号,ISNULL(F.MB002,'') 下阶品名,ISNULL(F.MB029,'') 下阶图号,CONVERT(CHAR(10),GETDATE(),111) 要求完工日期 FROM MOCTA B LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN BOMMG ON MF001=MG001 AND MF002=MG002 AND MF003=MG003 LEFT JOIN CMSMW C ON C.MW001=MF004 LEFT JOIN CMSMW D ON D.MW001=MGD01 LEFT JOIN V_SFCTE ON B.TA001=TE006 AND B.TA002=TE007 AND MF003=TE008 LEFT JOIN ITEM E ON E.ITEM_CODE=MF023 LEFT JOIN INVMB F ON MF023=F.MB001 WHERE  B.TA001='" + gridView1.GetFocusedRowCellValue("单别").ToString() + "' AND B.TA002='" + gridView1.GetFocusedRowCellValue("单号").ToString() + "' AND ISNULL(D.MW002,'') LIKE '%" + textBox2.Text + "%'";
                    }
                }
                DataTable dt;
                //textBox12.Text = strSql;
                db = new DataBase();
                dt = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    int index = this.dataGridView2.Rows.Add();
                    //MessageBox.Show(index.ToString(),"1");
                    this.dataGridView2.Rows[index].Cells[0].Value = false;
                    String XH = "000" + (index + 1).ToString();
                    XH = XH.Substring(XH.Length - 4, 4);
                    this.dataGridView2.Rows[index].Cells[1].Value = XH;
                    this.dataGridView2.Rows[index].Cells[2].Value = dt.Rows[i][1].ToString();
                    this.dataGridView2.Rows[index].Cells[3].Value = dt.Rows[i][2].ToString();
                    this.dataGridView2.Rows[index].Cells[4].Value = dt.Rows[i][3].ToString();
                    this.dataGridView2.Rows[index].Cells[5].Value = dt.Rows[i][4].ToString();
                    this.dataGridView2.Rows[index].Cells[6].Value = dt.Rows[i][5].ToString();
                    this.dataGridView2.Rows[index].Cells[7].Value = dt.Rows[i][6].ToString();
                    this.dataGridView2.Rows[index].Cells[8].Value = dt.Rows[i][7].ToString();
                    this.dataGridView2.Rows[index].Cells[9].Value = dt.Rows[i][8].ToString();
                    this.dataGridView2.Rows[index].Cells[10].Value = dt.Rows[i][9].ToString();
                    this.dataGridView2.Rows[index].Cells[11].Value = dt.Rows[i][10].ToString();
                    this.dataGridView2.Rows[index].Cells[12].Value = dt.Rows[i][11].ToString();
                    this.dataGridView2.Rows[index].Cells[13].Value = dt.Rows[i][12].ToString();
                    this.dataGridView2.Rows[index].Cells[14].Value = dt.Rows[i][13].ToString();
                    this.dataGridView2.Rows[index].Cells[15].Value = dt.Rows[i][14].ToString();
                    this.dataGridView2.Rows[index].Cells[16].Value = dt.Rows[i][15].ToString();
                    this.dataGridView2.Rows[index].Cells[17].Value = dt.Rows[i][16].ToString();
                    this.dataGridView2.Rows[index].Cells[18].Value = dt.Rows[i][17].ToString();
                    this.dataGridView2.Rows[index].Cells[19].Value = dt.Rows[i][18].ToString();
                    this.dataGridView2.Rows[index].Cells[20].Value = dt.Rows[i][19].ToString();
                    this.dataGridView2.Rows[index].Cells[21].Value = dt.Rows[i][20].ToString();
                    this.dataGridView2.Rows[index].Cells[22].Value = dt.Rows[i][21].ToString();
                    this.dataGridView2.Rows[index].Cells[23].Value = dt.Rows[i][22].ToString();
                    this.dataGridView2.Rows[index].Cells[24].Value = dt.Rows[i][23].ToString();
                    this.dataGridView2.Rows[index].Cells[25].Value = dt.Rows[i][24].ToString();
                    this.dataGridView2.Rows[index].Cells[26].Value = dt.Rows[i][25].ToString();
                }

            }
            catch
            {

            }
        }

        //private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        //{
        //}

        //private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        //{
        //    DataGridView grid = sender as DataGridView;
        //    if (grid != null)
        //    {
        //        grid.CommitEdit(DataGridViewDataErrorContexts.Commit);
        //    }
        //}

        //private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        //{
           

        //}

        private void GXBGD_Load(object sender, EventArgs e)
        {
            DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
            DatagridViewCheckBoxHeaderCell bbHeader = new DatagridViewCheckBoxHeaderCell();
            colCB.HeaderCell = bbHeader;
            colCB.Name = "colCB";
            colCB.HeaderText = "";
            //dataGridView1.Columns.Insert(0, colCB);
            //bbHeader.OnCheckBoxClicked +=
            //    new CheckBoxClickedHandler(abHeader_OnCheckBoxClicked);

            DataGridViewCheckBoxColumn colCC = new DataGridViewCheckBoxColumn();
            DatagridViewCheckBoxHeaderCell cbHeader = new DatagridViewCheckBoxHeaderCell();
            colCC.HeaderCell = cbHeader;
            colCC.Name = "colCC";
            colCC.HeaderText = "";
            dataGridView2.Columns.Insert(0, colCC);
            cbHeader.OnCheckBoxClicked +=
                new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);

            form1 = new GXBGDItem();
            form1.DataSelected += new EventHandler(j_DataSelected);
            form1.Hide();

            if(PropertyClass.jiaose =="统计员")
            {
                checkBox1.CheckState = CheckState.Unchecked;
                button2.Text = "报工";
                dataGridView2.Columns["YJCL"].HeaderText = "可报数量";
                dataGridView2.Columns["YBSL"].HeaderText = "已报数量";
                dataGridView2.Columns["WBSL"].HeaderText = "未报数量";
                dataGridView2.Columns["HGSL"].HeaderText = "合格数量";
                dataGridView2.Columns["GFSL"].Visible = true;
                dataGridView2.Columns["LFSL"].Visible = true;
                dataGridView2.Columns["BLSL"].Visible = true;
            }
            else
            {
                checkBox1.CheckState = CheckState.Unchecked;
                button2.Text = "派工";
                dataGridView2.Columns["YJCL"].HeaderText = "可派数量";
                dataGridView2.Columns["YBSL"].HeaderText = "已派数量";
                dataGridView2.Columns["WBSL"].HeaderText = "未派数量";
                dataGridView2.Columns["HGSL"].HeaderText = "派工数量";
                dataGridView2.Columns["GFSL"].Visible = false;
                dataGridView2.Columns["LFSL"].Visible = false;
                dataGridView2.Columns["BLSL"].Visible = false;
            }

            ////DateTimePicker dmg = new DateTimePicker();
            //dmg.Value = System.DateTime.Now;
            //this.dataGridView2.Controls.Add(dmg);
            //dmg.Visible = false;
        }

        
        private void cbHeader_OnCheckBoxClicked(bool state)
        {
            //这一句很重要结束编辑状态
            dataGridView2.EndEdit();
            dataGridView2.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void textBox2_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1.PerformClick();
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string strSql = null;
            string code = null;
            string date = dateTimePicker1.Value.ToShortDateString();
            

             //开始生成单据 单号DH
            if (checkBox1.CheckState == CheckState.Checked)
            {
                strSql = "SELECT ISNULL(CAST(CAST(MAX(TD002) AS NUMERIC(11))+1 AS CHAR(11)) ,CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112)+'001') AS DH FROM SFCTD WHERE TD001='D420' AND TD002 LIKE CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112) + '%'";
            }
            else
            {
                strSql = "SELECT ISNULL(CAST(CAST(MAX(TD002) AS NUMERIC(11))+1 AS CHAR(11)) ,CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112)+'001') AS DH FROM SFCTD WHERE TD001='D410' AND TD002 LIKE CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112) + '%'";
            }
            //textBox12.Text = strSql;
            //MessageBox.Show("开始生成单据", "提示");
            //return;
           
            try
            {
                sdr1 = db.GetDataReader1(strSql);

                while (sdr1.Read())
                {
                    code = sdr1["DH"].ToString();
                }

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }
            finally
            {
                sdr1.Close();
            }
            if (checkBox1.CheckState == CheckState.Checked)
            {
                strSql = "INSERT INTO SFCTD(CREATOR,CREATE_DATE,FLAG,TD001,TD002,TD003,TD004,TD005,TD006,TD007,TD008,TD009,TD010,TD011,TD012,TD013,TD014,TD015,TD016,TD017,TD018,TD019,TD020,UDF01)  SELECT '" + PropertyClass.OperatorCode + "',CONVERT(CHAR(20),GETDATE(),120),1,'D420','" + code + "',CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112),'" + textBox1.Text + "','Y','',0,CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112),'','N',0,'','','',0,0,0,'**********',0,'********************','白班'";
            }
            else
            {
                strSql = "INSERT INTO SFCTD(CREATOR,CREATE_DATE,FLAG,TD001,TD002,TD003,TD004,TD005,TD006,TD007,TD008,TD009,TD010,TD011,TD012,TD013,TD014,TD015,TD016,TD017,TD018,TD019,TD020,UDF01)  SELECT '" + PropertyClass.OperatorCode + "',CONVERT(CHAR(20),GETDATE(),120),1,'D410','" + code + "',CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112),'" + textBox1.Text + "','Y','',0,CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112),'','N',0,'','','',0,0,0,'**********',0,'********************','白班'";
            }
            //MessageBox.Show("插入SFCTD", "提示");
            //textBox12.Text = strSql;
            //return;
            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {
                }
                else
                {
                    MessageBox.Show("生成报工单头失败，请检查" + code, "软件提示");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
            foreach (DataGridViewRow dr in this.dataGridView2.Rows)
            {
                double s = Convert.ToDouble(dr.Cells["HGSL"].Value) + Convert.ToDouble(dr.Cells["GFSL"].Value) + Convert.ToDouble(dr.Cells["LFSL"].Value) + Convert.ToDouble(dr.Cells["BLSL"].Value); //数量
                string ygbh = (dr.Cells["YGBH"].Value == null) ? "" : dr.Cells["YGBH"].Value.ToString();
                string xh= (dr.Cells["XH"].Value == null) ? "" : dr.Cells["XH"].Value.ToString();
                string bz = (dr.Cells["BZ"].Value == null) ? "" : dr.Cells["BZ"].Value.ToString().Trim();
                string jjdj = (dr.Cells["JJDJ"].Value == null) ? "0.00" : dr.Cells["JJDJ"].Value.ToString().Trim();
                string GDDB = (dr.Cells["TA001"].Value == null) ? "" : dr.Cells["TA001"].Value.ToString();
                string GDDH = (dr.Cells["TA002"].Value == null) ? "" : dr.Cells["TA002"].Value.ToString();
                string GX = (dr.Cells["TA003"].Value == null) ? "" : dr.Cells["TA003"].Value.ToString();

                if (s >0.00)
                {
                    string bgdb = (dr.Cells["TE001"].Value == null) ? "" : dr.Cells["TE001"].Value.ToString();
                    string bgdh = (dr.Cells["TE002"].Value == null) ? "" : dr.Cells["TE002"].Value.ToString();
                    string bgxh = (dr.Cells["XH"].Value == null) ? "" : dr.Cells["XH"].Value.ToString();
                    string zrgx= (dr.Cells["ZRGX"].Value == null) ? "" : dr.Cells["ZRGX"].Value.ToString();
                   
                    if (checkBox1.CheckState == CheckState.Unchecked)
                    {
                        if (ygbh == "")
                        {
                            MessageBox.Show("员工编号为空，不可报工！", "系统提示");
                            return;
                        }
                    }

                    int k = 0;
                    if (dt != null)
                    {
                        for (int i = 0; i <= dt.Rows.Count - 1; i++)
                        {
                            if (dt.Rows[i]["XH"].ToString() == xh)
                            {
                                if (checkBox1.CheckState == CheckState.Checked)
                                {
                                    if(jjdj=="")
                                    {
                                        strSql = "INSERT INTO SFCTE(CREATE_DATE,FLAG,TE001,TE002,TE003,TE004,TE005,TE006,TE007,TE008,TE009,TE010,TE011,TE012,TE013,TE014,TE015,TE016,TE017,TE018,TE019,TE020,TE021,TE022,TE023,TE024,TE025,TE026,TE027,TE028,TE029,TE030,TE031,TE032,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF07,UDF08,UDF09,UDF10,UDF11,UDF12,UDF57,UDF58,UDF59,UDF60) SELECT REPLACE(REPLACE(REPLACE(CONVERT(CHAR(20),GETDATE(),120),'-',''),':',''),' ',''),1,TD001,TD002,RIGHT(RTRIM('000'+CAST(MAX(ISNULL(TE003,0))+1 AS CHAR(4))),4),'" + dt.Rows[i]["ZYYG"].ToString() + "','','" + dr.Cells["TA001"].Value + "','" + dr.Cells["TA002"].Value + "','" + dr.Cells["TA003"].Value + "',A.MF004,'1',0,0,0,'Y','" + bz + "'+'" + dt.Rows[i]["BZ"].ToString().Trim() + "',0,B.TA006,B.TA034,B.TA035,MB004,'','',CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112),'',0,0,0,0,0,'1',0,'********************',0,0,";
                                        strSql = strSql + "0,0,0,0,'" + dr.Cells["GYSM"].Value + "','','" + zrgx + "','" + code + "','" + code + "','" + dr.Cells["MF008"].Value + "','" + dt.Rows[i]["YGXM"].ToString() + "','" + dr.Cells["MG006"].Value + "','','','',''," + dt.Rows[i]["OKQTY"].ToString() + ",0,0,0 FROM SFCTD LEFT JOIN SFCTE ON TD001=TE001 AND TD002=TE002,MOCTA B LEFT JOIN INVMB ON B.TA006=MB001 LEFT JOIN BOMMF A ON MB010=MF001 AND MB011=MF002 WHERE TD001='D420' AND TD002='" + code + "' AND B.TA001='" + dr.Cells["TA001"].Value + "' AND B.TA002='" + dr.Cells["TA002"].Value + "'  AND A.MF003='" + dr.Cells["TA003"].Value + "' GROUP BY TD001,TD002,A.MF004,B.TA006,B.TA034,B.TA035,MB004";
                                    }
                                    else
                                    {
                                        strSql = "INSERT INTO SFCTE(CREATE_DATE,FLAG,TE001,TE002,TE003,TE004,TE005,TE006,TE007,TE008,TE009,TE010,TE011,TE012,TE013,TE014,TE015,TE016,TE017,TE018,TE019,TE020,TE021,TE022,TE023,TE024,TE025,TE026,TE027,TE028,TE029,TE030,TE031,TE032,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF07,UDF08,UDF09,UDF10,UDF11,UDF12,UDF57,UDF58,UDF59,UDF60) SELECT REPLACE(REPLACE(REPLACE(CONVERT(CHAR(20),GETDATE(),120),'-',''),':',''),' ',''),1,TD001,TD002,RIGHT(RTRIM('000'+CAST(MAX(ISNULL(TE003,0))+1 AS CHAR(4))),4),'" + dt.Rows[i]["ZYYG"].ToString() + "','','" + dr.Cells["TA001"].Value + "','" + dr.Cells["TA002"].Value + "','" + dr.Cells["TA003"].Value + "',A.MF004,'1',0,0,0,'Y','" + bz + "'+'" + dt.Rows[i]["BZ"].ToString().Trim() + "',0,B.TA006,B.TA034,B.TA035,MB004,'','',CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112),'',0,0,0," + jjdj + ",0,'1',0,'********************',0,0,";
                                        strSql = strSql + "0,0,0,0,'" + dr.Cells["GYSM"].Value + "','','" + zrgx + "','" + code + "','" + code + "','" + dr.Cells["MF008"].Value + "','" + dt.Rows[i]["YGXM"].ToString() + "','" + dr.Cells["MG006"].Value + "','','','',''," + dt.Rows[i]["OKQTY"].ToString() + ",0,0,0 FROM SFCTD LEFT JOIN SFCTE ON TD001=TE001 AND TD002=TE002,MOCTA B LEFT JOIN INVMB ON B.TA006=MB001 LEFT JOIN BOMMF A ON MB010=MF001 AND MB011=MF002 WHERE TD001='D420' AND TD002='" + code + "' AND B.TA001='" + dr.Cells["TA001"].Value + "' AND B.TA002='" + dr.Cells["TA002"].Value + "'  AND A.MF003='" + dr.Cells["TA003"].Value + "' GROUP BY TD001,TD002,A.MF004,B.TA006,B.TA034,B.TA035,MB004";
                                    }

                                }
                                else
                                {
                                    if (jjdj == "")
                                    {
                                        strSql = "INSERT INTO SFCTE(CREATE_DATE,FLAG,TE001,TE002,TE003,TE004,TE005,TE006,TE007,TE008,TE009,TE010,TE011,TE012,TE013,TE014,TE015,TE016,TE017,TE018,TE019,TE020,TE021,TE022,TE023,TE024,TE025,TE026,TE027,TE028,TE029,TE030,TE031,TE032,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF07,UDF08,UDF09,UDF10,UDF11,UDF12,UDF57,UDF58,UDF59,UDF60) SELECT REPLACE(REPLACE(REPLACE(CONVERT(CHAR(20),GETDATE(),120),'-',''),':',''),' ',''),1,TD001,TD002,RIGHT(RTRIM('000'+CAST(MAX(ISNULL(TE003,0))+1 AS CHAR(4))),4),'" + dt.Rows[i]["ZYYG"].ToString() + "','','" + dr.Cells["TA001"].Value + "','" + dr.Cells["TA002"].Value + "','" + dr.Cells["TA003"].Value + "',A.MF004,'1'," + dt.Rows[i]["OKQTY"].ToString() + ",0,0,'Y','" + bz + "'+'" + dt.Rows[i]["BZ"].ToString().Trim() + "',0,B.TA006,B.TA034,B.TA035,MB004,'','',CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112),'',0,0,0,0,0,'1',0,'********************'," + dt.Rows[i]["MreQTY"].ToString() + "," + dt.Rows[i]["PreQTY"].ToString() + ",";
                                        strSql = strSql + "" + dt.Rows[i]["NGQTY"].ToString() + ",0,0,0,'" + dr.Cells["GYSM"].Value + "','','" + zrgx + "','" + code + "','" + code + "','" + dr.Cells["MF008"].Value + "','" + dt.Rows[i]["YGXM"].ToString() + "','" + dr.Cells["MG006"].Value + "','" + bgdb + "','" + bgdh + "','" + bgxh + "','',0,0,0,0 FROM SFCTD LEFT JOIN SFCTE ON TD001=TE001 AND TD002=TE002,MOCTA B LEFT JOIN INVMB ON B.TA006=MB001 LEFT JOIN BOMMF A ON MB010=MF001 AND MB011=MF002 WHERE TD001='D410' AND TD002='" + code + "' AND B.TA001='" + dr.Cells["TA001"].Value + "' AND B.TA002='" + dr.Cells["TA002"].Value + "'  AND A.MF003='" + dr.Cells["TA003"].Value + "' GROUP BY TD001,TD002,A.MF004,B.TA006,B.TA034,B.TA035,MB004";

                                    }
                                    else
                                    {
                                        strSql = "INSERT INTO SFCTE(CREATE_DATE,FLAG,TE001,TE002,TE003,TE004,TE005,TE006,TE007,TE008,TE009,TE010,TE011,TE012,TE013,TE014,TE015,TE016,TE017,TE018,TE019,TE020,TE021,TE022,TE023,TE024,TE025,TE026,TE027,TE028,TE029,TE030,TE031,TE032,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF07,UDF08,UDF09,UDF10,UDF11,UDF12,UDF57,UDF58,UDF59,UDF60) SELECT REPLACE(REPLACE(REPLACE(CONVERT(CHAR(20),GETDATE(),120),'-',''),':',''),' ',''),1,TD001,TD002,RIGHT(RTRIM('000'+CAST(MAX(ISNULL(TE003,0))+1 AS CHAR(4))),4),'" + dt.Rows[i]["ZYYG"].ToString() + "','','" + dr.Cells["TA001"].Value + "','" + dr.Cells["TA002"].Value + "','" + dr.Cells["TA003"].Value + "',A.MF004,'1'," + dt.Rows[i]["OKQTY"].ToString() + ",0,0,'Y','" + bz + "'+'" + dt.Rows[i]["BZ"].ToString().Trim() + "',0,B.TA006,B.TA034,B.TA035,MB004,'','',CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112),'',0,0,0," + jjdj + ",0,'1',0,'********************'," + dt.Rows[i]["MreQTY"].ToString() + "," + dt.Rows[i]["PreQTY"].ToString() + ",";
                                        strSql = strSql + "" + dt.Rows[i]["NGQTY"].ToString() + ",0,0,0,'" + dr.Cells["GYSM"].Value + "','','" + zrgx + "','" + code + "','" + code + "','" + dr.Cells["MF008"].Value + "','" + dt.Rows[i]["YGXM"].ToString() + "','" + dr.Cells["MG006"].Value + "','" + bgdb + "','" + bgdh + "','" + bgxh + "','',0,0,0,0 FROM SFCTD LEFT JOIN SFCTE ON TD001=TE001 AND TD002=TE002,MOCTA B LEFT JOIN INVMB ON B.TA006=MB001 LEFT JOIN BOMMF A ON MB010=MF001 AND MB011=MF002 WHERE TD001='D410' AND TD002='" + code + "' AND B.TA001='" + dr.Cells["TA001"].Value + "' AND B.TA002='" + dr.Cells["TA002"].Value + "'  AND A.MF003='" + dr.Cells["TA003"].Value + "' GROUP BY TD001,TD002,A.MF004,B.TA006,B.TA034,B.TA035,MB004";
                                    }
                                }
                                //textBox13.Text = strSql;
                                try
                                {
                                    if (db.ExecDataBySql1(strSql) > 0)
                                    {
                                        k += 1;
                                    }
                                    else
                                    {
                                    }
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show("生成报工单身失败，请检查" + code, "软件提示");
                                    return;
                                }
                            }
                        }
                    }
                    if(k==0)
                    {
                        if (checkBox1.CheckState == CheckState.Checked)
                        {
                            if (jjdj == "")
                            {
                                strSql = "INSERT INTO SFCTE(CREATE_DATE,FLAG,TE001,TE002,TE003,TE004,TE005,TE006,TE007,TE008,TE009,TE010,TE011,TE012,TE013,TE014,TE015,TE016,TE017,TE018,TE019,TE020,TE021,TE022,TE023,TE024,TE025,TE026,TE027,TE028,TE029,TE030,TE031,TE032,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF07,UDF08,UDF09,UDF10,UDF11,UDF12,UDF57,UDF58,UDF59,UDF60) SELECT REPLACE(REPLACE(REPLACE(CONVERT(CHAR(20),GETDATE(),120),'-',''),':',''),' ',''),1,TD001,TD002,RIGHT(RTRIM('000'+CAST(MAX(ISNULL(TE003,0))+1 AS CHAR(4))),4),'" + dr.Cells["YGBH"].Value + "','','" + dr.Cells["TA001"].Value + "','" + dr.Cells["TA002"].Value + "','" + dr.Cells["TA003"].Value + "',A.MF004,'1',0,0,0,'Y','" + dr.Cells["BZ"].Value + "',0,B.TA006,B.TA034,B.TA035,MB004,'','',CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112),'',0,0,0,0,0,'1',0,'********************',0,0,";
                                strSql = strSql + "0,0,0,0,'" + dr.Cells["MF008"].Value + "','','','" + code + "','" + code + "','" + dr.Cells["GYSM"].Value + "','" + dr.Cells["YGXM"].Value + "','" + dr.Cells["MG006"].Value + "','','','',''," + dr.Cells["HGSL"].Value + ",0,0,0 FROM SFCTD LEFT JOIN SFCTE ON TD001=TE001 AND TD002=TE002,MOCTA B LEFT JOIN INVMB ON B.TA006=MB001 LEFT JOIN BOMMF A ON MB010=MF001 AND MB011=MF002 WHERE TD001='D420' AND TD002='" + code + "' AND B.TA001='" + dr.Cells["TA001"].Value + "' AND B.TA002='" + dr.Cells["TA002"].Value + "'  AND A.MF003='" + dr.Cells["TA003"].Value + "' GROUP BY TD001,TD002,A.MF004,B.TA006,B.TA034,B.TA035,MB004";
                            }
                            else
                            {
                                strSql = "INSERT INTO SFCTE(CREATE_DATE,FLAG,TE001,TE002,TE003,TE004,TE005,TE006,TE007,TE008,TE009,TE010,TE011,TE012,TE013,TE014,TE015,TE016,TE017,TE018,TE019,TE020,TE021,TE022,TE023,TE024,TE025,TE026,TE027,TE028,TE029,TE030,TE031,TE032,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF07,UDF08,UDF09,UDF10,UDF11,UDF12,UDF57,UDF58,UDF59,UDF60) SELECT REPLACE(REPLACE(REPLACE(CONVERT(CHAR(20),GETDATE(),120),'-',''),':',''),' ',''),1,TD001,TD002,RIGHT(RTRIM('000'+CAST(MAX(ISNULL(TE003,0))+1 AS CHAR(4))),4),'" + dr.Cells["YGBH"].Value + "','','" + dr.Cells["TA001"].Value + "','" + dr.Cells["TA002"].Value + "','" + dr.Cells["TA003"].Value + "',A.MF004,'1',0,0,0,'Y','" + dr.Cells["BZ"].Value + "',0,B.TA006,B.TA034,B.TA035,MB004,'','',CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112),'',0,0,0," + jjdj + ",0,'1',0,'********************',0,0,";
                                strSql = strSql + "0,0,0,0,'" + dr.Cells["MF008"].Value + "','','','" + code + "','" + code + "','" + dr.Cells["GYSM"].Value + "','" + dr.Cells["YGXM"].Value + "','" + dr.Cells["MG006"].Value + "','','','',''," + dr.Cells["HGSL"].Value + ",0,0,0 FROM SFCTD LEFT JOIN SFCTE ON TD001=TE001 AND TD002=TE002,MOCTA B LEFT JOIN INVMB ON B.TA006=MB001 LEFT JOIN BOMMF A ON MB010=MF001 AND MB011=MF002 WHERE TD001='D420' AND TD002='" + code + "' AND B.TA001='" + dr.Cells["TA001"].Value + "' AND B.TA002='" + dr.Cells["TA002"].Value + "'  AND A.MF003='" + dr.Cells["TA003"].Value + "' GROUP BY TD001,TD002,A.MF004,B.TA006,B.TA034,B.TA035,MB004";
                            }
                        }
                        else
                        {
                            if (jjdj == "")
                            {
                                strSql = "INSERT INTO SFCTE(CREATE_DATE,FLAG,TE001,TE002,TE003,TE004,TE005,TE006,TE007,TE008,TE009,TE010,TE011,TE012,TE013,TE014,TE015,TE016,TE017,TE018,TE019,TE020,TE021,TE022,TE023,TE024,TE025,TE026,TE027,TE028,TE029,TE030,TE031,TE032,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF07,UDF08,UDF09,UDF10,UDF11,UDF12,UDF57,UDF58,UDF59,UDF60) SELECT REPLACE(REPLACE(REPLACE(CONVERT(CHAR(20),GETDATE(),120),'-',''),':',''),' ',''),1,TD001,TD002,RIGHT(RTRIM('000'+CAST(MAX(ISNULL(TE003,0))+1 AS CHAR(4))),4),'" + dr.Cells["YGBH"].Value + "','','" + dr.Cells["TA001"].Value + "','" + dr.Cells["TA002"].Value + "','" + dr.Cells["TA003"].Value + "',A.MF004,'1'," + dr.Cells["HGSL"].Value + ",0,0,'Y','" + dr.Cells["BZ"].Value + "',0,B.TA006,B.TA034,B.TA035,MB004,'','',CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112),'',0,0,0,0,0,'1',0,'********************'," + dr.Cells["LFSL"].Value + "," + dr.Cells["GFSL"].Value + ",";
                                strSql = strSql + "" + dr.Cells["BLSL"].Value + ",0,0,0,'" + dr.Cells["MF008"].Value + "','','','" + code + "','" + code + "','" + dr.Cells["GYSM"].Value + "','" + dr.Cells["YGXM"].Value + "','" + dr.Cells["MG006"].Value + "','" + bgdb + "','" + bgdh + "','" + bgxh + "','',0,0,0,0 FROM SFCTD LEFT JOIN SFCTE ON TD001=TE001 AND TD002=TE002,MOCTA B LEFT JOIN INVMB ON B.TA006=MB001 LEFT JOIN BOMMF A ON MB010=MF001 AND MB011=MF002 WHERE TD001='D410' AND TD002='" + code + "' AND B.TA001='" + dr.Cells["TA001"].Value + "' AND B.TA002='" + dr.Cells["TA002"].Value + "'  AND A.MF003='" + dr.Cells["TA003"].Value + "' GROUP BY TD001,TD002,A.MF004,B.TA006,B.TA034,B.TA035,MB004";
                            }
                            else
                            {
                                strSql = "INSERT INTO SFCTE(CREATE_DATE,FLAG,TE001,TE002,TE003,TE004,TE005,TE006,TE007,TE008,TE009,TE010,TE011,TE012,TE013,TE014,TE015,TE016,TE017,TE018,TE019,TE020,TE021,TE022,TE023,TE024,TE025,TE026,TE027,TE028,TE029,TE030,TE031,TE032,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF07,UDF08,UDF09,UDF10,UDF11,UDF12,UDF57,UDF58,UDF59,UDF60) SELECT REPLACE(REPLACE(REPLACE(CONVERT(CHAR(20),GETDATE(),120),'-',''),':',''),' ',''),1,TD001,TD002,RIGHT(RTRIM('000'+CAST(MAX(ISNULL(TE003,0))+1 AS CHAR(4))),4),'" + dr.Cells["YGBH"].Value + "','','" + dr.Cells["TA001"].Value + "','" + dr.Cells["TA002"].Value + "','" + dr.Cells["TA003"].Value + "',A.MF004,'1'," + dr.Cells["HGSL"].Value + ",0,0,'Y','" + dr.Cells["BZ"].Value + "',0,B.TA006,B.TA034,B.TA035,MB004,'','',CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112),'',0,0,0," + jjdj + ",0,'1',0,'********************'," + dr.Cells["LFSL"].Value + "," + dr.Cells["GFSL"].Value + ",";
                                strSql = strSql + "" + dr.Cells["BLSL"].Value + ",0,0,0,'" + dr.Cells["MF008"].Value + "','','','" + code + "','" + code + "','" + dr.Cells["GYSM"].Value + "','" + dr.Cells["YGXM"].Value + "','" + dr.Cells["MG006"].Value + "','" + bgdb + "','" + bgdh + "','" + bgxh + "','',0,0,0,0 FROM SFCTD LEFT JOIN SFCTE ON TD001=TE001 AND TD002=TE002,MOCTA B LEFT JOIN INVMB ON B.TA006=MB001 LEFT JOIN BOMMF A ON MB010=MF001 AND MB011=MF002 WHERE TD001='D410' AND TD002='" + code + "' AND B.TA001='" + dr.Cells["TA001"].Value + "' AND B.TA002='" + dr.Cells["TA002"].Value + "'  AND A.MF003='" + dr.Cells["TA003"].Value + "' GROUP BY TD001,TD002,A.MF004,B.TA006,B.TA034,B.TA035,MB004";
                            }
                        }
                        MessageBox.Show("insert", "提示");
                        textBox12.Text = strSql;

                        try
                        {
                            if (db.ExecDataBySql1(strSql) > 0)
                            {
                            }
                            else
                            {
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("生成报工单身失败，请检查" + code, "软件提示");
                            return;
                        }
                    }


                    if (bgdb != "")  //如果派工单别不为空则回写派工单
                    {
                        double ybsl = Convert.ToDouble(dr.Cells["HGSL"].Value) + Convert.ToDouble(dr.Cells["BLSL"].Value) + Convert.ToDouble(dr.Cells["GFSL"].Value) + Convert.ToDouble(dr.Cells["LFSL"].Value);
                        //MessageBox.Show(ybsl.ToString(), "提示");
                        //标记
                        strSql = "UPDATE SFCTE SET UDF58 = UDF58+" + ybsl.ToString() + " WHERE TE006='" + bgdb + "' AND TE007='" + bgdh + "' AND TE003='" + bgxh + "'";
                        //strSql = "UPDATE SFCTE SET UDF58 = UDF58+" + ybsl.ToString() + " WHERE TE006='" + bgdb + "' AND TE007='" + bgdh + "' AND TE008='" + bgxh + "'";
                        //MessageBox.Show("回写报工单", "提示");
                        //textBox12.Text = strSql;
                        //return;

                        try
                        {
                            if (db.ExecDataBySql1(strSql) > 0)
                            {
                                //MessageBox.Show("回写报工单", "提示");
                            }
                            else
                            {
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("生成报工单身失败，请检查" + code, "软件提示");
                            return;
                        }
                    }                
                }
                else
                {
                    MessageBox.Show("报工数量为0，不可报工！", "系统提示");
                    return;
                }
                //MessageBox.Show("mocty", "ts");
                //textBox12.Text = strSql;
                
                strSql = "UPDATE MOCTY SET TY006=TY006+" + s.ToString() + " WHERE TY001='" + GDDB + "' AND TY002='" + GDDH + "' AND TY019='" + GX + "'";
                //MessageBox.Show("mocty", "ts");
                //textBox12.Text = strSql;
                
                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {
                    }
                    else
                    {
                        //MessageBox.Show("生成报工单身失败，请检查", "软件提示");
                        //return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }

            }
            if (dt != null)
            {
                dt.Rows.Clear();
            }
            button1.PerformClick();
            this.dataGridView2.Controls.Clear();//移除所有控件 
            if (checkBox1.CheckState == CheckState.Checked)
            {
                MessageBox.Show("派工成功，单号为" + code, "软件提示");
            }
            else
            {
                MessageBox.Show("报工成功，单号为" + code, "软件提示");
            }
          
            if (checkBox1.CheckState==CheckState.Checked)
            {
                string x1 = "D420";
                string x2 = code;
                string x3 = "";
                SJCD xx = new SJCD(x1, x2, x3);
                LabelPrint L = new LabelPrint();
                L.cdssh = new LabelPrint.Cdssh(textBox2_TextChanged);
                L.Show();
            }
            try
            {
                form1.Dispose();
                form1.Close();
                form1 = new GXBGDItem();
                form1.DataSelected += new EventHandler(j_DataSelected);
                form1.Hide();
            }
            catch
            {

            }
           
        }

        private void panel4_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存为Excel文件";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string savePath = string.Empty;
                savePath = dlg.FileName;
                //需要添加 Microsoft.Office.Interop.Excel引用 
                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                if (null == app)
                {
                    MessageBox.Show("缺少Excel组件！");
                    return;
                }
                app.Visible = false;
                app.UserControl = true;
                Microsoft.Office.Interop.Excel.Workbook workbook = app.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                // workbook.Sheets.Add();
                try
                {
                    Microsoft.Office.Interop.Excel.Worksheet worksheet = (MSword.Worksheet)workbook.Worksheets[1];
                    // worksheet.Name = "Sheet1";
                    worksheet.Cells.NumberFormatLocal = "@";
                    for (int i = 0; i < dataGridView3.Columns.Count; i++)
                    {
                        worksheet.Cells[1, i + 1] = dataGridView3.Columns[i].Name;
                    }
                    // 保存数据
                    progressBar1.Visible = true;
                    progressBar1.Maximum = dataGridView3.Rows.Count;//设置最大长度值
                    progressBar1.Value = 0;//设置当前值
                    progressBar1.Step = 1;//设置没次增长多少
                    for (int r = 0; r < dataGridView3.Rows.Count; r++)
                    {
                        for (int i = 0; i < dataGridView3.Columns.Count; i++)
                        {
                            worksheet.Cells[r + 2, i + 1] = dataGridView3.Rows[r].Cells[i].Value; ;
                        }
                        progressBar1.Value += progressBar1.Step;
                    }
                    progressBar1.Visible = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                workbook.SaveAs(savePath);
                // 释放资源
                workbook.Close();
                workbook = null;
                app.Quit();
                app = null;
                MessageBox.Show("导出成功", "系统提示");
            }
        }

        private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string s = (this.dataGridView2.CurrentCell.Value == null) ? "" : this.dataGridView2.CurrentCell.Value.ToString();
                if (s != "")
                {
                    //员工编号YBBH 员工姓名YGXM
                    if (e.ColumnIndex.Equals(this.dataGridView2.Columns["YGBH"].Index))
                    {
                        string strSql = null;
                        strSql = "SELECT MV001,MV002 FROM CMSMV WHERE MV001='" + this.dataGridView2.CurrentCell.Value + "' OR MV002 LIKE '%" + this.dataGridView2.CurrentCell.Value + "%'";
                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["YGBH"].Index].Value = sdr1["MV001"].ToString();
                                this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["YGXM"].Index].Value = sdr1["MV002"].ToString();

                            }

                            if (sdr1.HasRows == false && this.dataGridView2.CurrentCell.Value.ToString().IndexOf(",") <= 0)
                            {
                                MessageBox.Show("该员工编号不存在！", "软件提示");
                                this.dataGridView2.CurrentCell.Value = "";
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            //throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }
                    }
                    //合格数量 HGSL  不良数量 BLSL 工废数量GFSL  料废数量 LFSL
                    if (e.ColumnIndex.Equals(this.dataGridView2.Columns["HGSL"].Index) || e.ColumnIndex.Equals(this.dataGridView2.Columns["BLSL"].Index) || e.ColumnIndex.Equals(this.dataGridView2.Columns["GFSL"].Index) || e.ColumnIndex.Equals(this.dataGridView2.Columns["LFSL"].Index))
                    {
                        try
                        {
                            double sgxwb = 0.00;
                            // 检查事件发生的行是否大于0（即不是第一行）
                            if (e.RowIndex>0)
                            {
                                // 如果不是第一行，获取当前行的上一行中名为"HGSL"的列的值，并转换为double类型
                                // 这个值被赋给变量sgxwb
                                sgxwb = Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex-1].Cells[this.dataGridView2.Columns["HGSL"].Index].Value);
                            }
                            // 循环遍历当前行之后的所有行
                            for (int i=e.RowIndex+1;i<=dataGridView2.Rows.Count-1;i++)
                            {
                                // 检查每行的"HGSL"列的值是否大于事件发生行的"HGSL"列的值
                                if (Convert.ToDouble(dataGridView2.Rows[i].Cells["HGSL"].Value)>Convert.ToDouble(dataGridView2.Rows[e.RowIndex].Cells["HGSL"].Value))
                                {
                                    // 如果是，将这些行的"HGSL"列的值更新为事件发生行的"HGSL"列的值
                                    dataGridView2.Rows[i].Cells["HGSL"].Value = dataGridView2.Rows[e.RowIndex].Cells["HGSL"].Value.ToString();
                                }
                            }
                            //可报数量YJCL 合格数量 HGSL  不良数量 BLSL 工废数量GFSL  料废数量 LFSL
                            if (Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["HGSL"].Index].Value) + Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["GFSL"].Index].Value) + Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["LFSL"].Index].Value) + Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["BLSL"].Index].Value) + Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["YBSL"].Index].Value) > (Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["YJCL"].Index].Value)+sgxwb))
                            {
                                MessageBox.Show("数量超报！", "软件提示");
                                this.dataGridView2.CurrentCell.Value = 0;
                            }
                            if (Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["HGSL"].Index].Value) + Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["GFSL"].Index].Value) + Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["LFSL"].Index].Value) + Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["BLSL"].Index].Value) > 0.00)
                            {

                            }
                            //double jggs = Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["HGSL"].Index].Value) * Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["JJDJ"].Index].Value) / 3600.00;
                            //this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["JGGS"].Index].Value = jggs.ToString();

                            //if (Convert.ToDouble(dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["SCDE"].Index].Value) > 0.00)
                            //{
                            //    double scbs = Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["HGSL"].Index].Value) / Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["SCDE"].Index].Value);
                            //    this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["SCBS"].Index].Value = scbs.ToString();

                            //}
                        }
                        catch
                        {
                        }
                    }
                }
                if (e.ColumnIndex.Equals(this.dataGridView2.Columns["HGSL"].Index) && checkBox1.CheckState==CheckState.Checked)
                {
                    try
                    {
                        double pgsl = 0.00;
                        double kpsl = 0.00;
                        double ypsl = 0.00;

                        if (e.RowIndex > 0)
                        {
                            pgsl = Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["HGSL"].Index].Value);
                            kpsl = Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["YJCL"].Index].Value);
                            ypsl = Convert.ToDouble(this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["YBSL"].Index].Value);
                       


                        if (pgsl+ypsl>kpsl)
                        {
                            MessageBox.Show("数量超派！", "软件提示");
                            this.dataGridView2.CurrentCell.Value = 0;
                        }
                      }
                    }
                    catch
                    {
                    }
                }

                s = (this.dataGridView2.CurrentCell.Value.ToString() == null) ? "" : this.dataGridView2.CurrentCell.Value.ToString();
                if (s == "")
                {
                    if (e.ColumnIndex.Equals(this.dataGridView2.Columns["YGBH"].Index))
                    {
                        this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["YGXM"].Index].Value = "";
                        this.dataGridView2.Rows[e.RowIndex].Cells[this.dataGridView2.Columns["YGBH"].Index].Value = "";
                    }
                }
            }
            catch
            {

            }
        }

        private void dataGridView2_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.dataGridView2.Controls.Clear();//移除所有控件 
                if (e.ColumnIndex.Equals(this.dataGridView2.Columns["YGBH"].Index))
                //if (e.ColumnIndex == 13)
                //判断单元格是否是"Company"列？    
                {
                    System.Windows.Forms.Button btn = new System.Windows.Forms.Button();//创建Buttonbtn    
                    btn.Text = "...";//设置button文字    
                    btn.Font = new System.Drawing.Font("Arial", 5);//设置文字格式    
                    btn.Visible = true;//设置控件允许显示    

                    btn.Width = this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex,
                                    e.RowIndex, true).Height;//获取单元格高并设置为btn的宽    
                    btn.Height = this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex,
                                    e.RowIndex, true).Height;//获取单元格高并设置为btn的高    

                    btn.Click += new EventHandler(btn_Click);//为btn添加单击事件    

                    this.dataGridView2.Controls.Add(btn);//dataGridView1中添加控件btn    

                    btn.Location = new System.Drawing.Point(((this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Right) -
                            (btn.Width)), this.dataGridView2.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y);//设置btn显示位置    
                }
                if (checkBox1.CheckState == CheckState.Checked)
                {
                    if (e.RowIndex >= 1)
                    {
                        if (Convert.ToDouble(dataGridView2.Rows[e.RowIndex].Cells["HGSL"].Value) == 0 && Convert.ToDouble(dataGridView2.Rows[e.RowIndex - 1].Cells["HGSL"].Value) > 0)
                        {
                            dataGridView2.Rows[e.RowIndex].Cells["HGSL"].Value = dataGridView2.Rows[e.RowIndex - 1].Cells["HGSL"].Value.ToString();
                        }
                    }
                    String zyyg = (dataGridView2.Rows[e.RowIndex + 1].Cells["YGBH"].Value == null) ? "" : dataGridView2.Rows[e.RowIndex + 1].Cells["YGBH"].Value.ToString();
                    if (Convert.ToDouble(dataGridView2.Rows[e.RowIndex + 1].Cells["HGSL"].Value) > 0 && zyyg == "")
                    {
                        dataGridView2.Rows[e.RowIndex + 1].Cells["HGSL"].Value = "0";
                    }
                }
            }
            catch
            {

            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            //string x1 = dataGridView1["工作中心", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            //SCGY xx = new SCGY(x1);
            f = new DDKC();
            f.kclx = "yuangong";
            f.DataSelected += new EventHandler(f_DataSelected);
            //f.cdgzzx += new SCKC.Cdgzzx(textBox2_TextChanged);
            f.Show(this);


        }

        void f_DataSelected(object sender, EventArgs e)
        {
          
                this.dataGridView2.Rows[this.dataGridView2.CurrentCell.RowIndex].Cells[this.dataGridView2.Columns["YGBH"].Index].Value = f.UserId;
                this.dataGridView2.Rows[this.dataGridView2.CurrentCell.RowIndex].Cells[this.dataGridView2.Columns["YGXM"].Index].Value = f.UserName;
             
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string strSql = null;
            //strSql = "SELECT TD003 报工日期,RTRIM(A.UDF12) PGDH,TE004 员工编号,MV002 员工姓名,TE006 工单单别,TE007 工单单号,TA027 订单号,TE017 品号,TE018 品名,TE019 规格,RTRIM(A.UDF06) AS 工作内容,CEILING(TE011) 数量,CEILING(A.UDF51) 料废数量,CEILING(A.UDF52) 工废数量,CEILING(A.UDF53) 不良数量,TE001 单别,TE002 单号,TE003 序号 FROM SFCTE A LEFT JOIN SFCTD ON TE001=TD001 AND TE002=TD002 LEFT JOIN CMSMV ON TE004=MV001 LEFT JOIN MOCTA ON TE006=TA001 AND TE007=TA002 WHERE TD003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker4.Value.ToShortDateString() + "'),112) AND TD003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker3.Value.ToShortDateString() + "'),112) AND (TA021='" + textBox10.Text + "' OR '" + textBox10.Text + "'='') AND (TA034 LIKE '%" + textBox8.Text + "%' OR '" + textBox8.Text + "'='') AND (TA027 LIKE '%" + textBox4.Text + "%' OR '" + textBox4.Text + "'='') AND (A.UDF01 LIKE '%" + textBox9.Text + "%' OR '" + textBox9.Text + "'='')  AND (A.UDF06 LIKE '%" + textBox7.Text + "%' OR '" + textBox7.Text + "'='')  AND (MV002 LIKE '%" + textBox11.Text + "%' OR '" + textBox11.Text + "'='')  ORDER BY A.TE003";
            //strSql = "SELECT TE023 生产日期,SFCTD.TD003 报工日期,ISNULL((SELECT  TOP 1 B.CREATE_DATE FROM SFCTE B WHERE B.TE006=A.TE006 AND B.TE007=A.TE007 AND B.TE008=A.TE008 AND B.TE001='D420'),'') 派工时间,A.CREATE_DATE 报工时间,ISNULL(SFCTD.UDF01,'') 班次,RTRIM(TE004) 员工编号,RTRIM(MV002) 员工姓名,RTRIM(MV020) 微信账号,TE006 工单单别,TE007 工单单号,RTRIM(TE017) 品号,RTRIM(TE018) 品名,RTRIM(TE019) 规格,RTRIM(MW002) 工艺,RTRIM(A.UDF06) 工艺说明,RTRIM(A.TE015) 备注,ISNULL(COPTD.TD002,'') 订单号,CAST(ISNULL(COPTD.TD008,0) AS REAL) 订单数量,CAST(TE011 AS REAL) 数量,CEILING(A.UDF51) 料废,CEILING(A.UDF52) 工废,CEILING(A.UDF53) 不良,CAST(MGD09 AS real) 计件单价,CAST(TE028 AS real) 报工时单价,CAST(ROUND(TE028*TE011,2) AS REAL) 金额,TA083 项目号,TE001 单别,TE002 单号,TE003 序号,SFCTD.CREATOR 报工账号 FROM SFCTE A LEFT JOIN SFCTD ON TE001=SFCTD.TD001 AND TE002=SFCTD.TD002 LEFT JOIN INVMB ON TE017=MB001 LEFT JOIN BOMMG ON MB010=MG001 AND MB011=MG002 AND TE008=MG003 AND A.UDF08=MG006 LEFT JOIN CMSMV ON TE004=MV001 LEFT JOIN CMSMW ON TE009=MW001 LEFT JOIN MOCTA ON TE006=TA001 AND TE007=TA002 LEFT JOIN COPTD ON TA026=COPTD.TD001 AND TA027=COPTD.TD002 AND TA028=COPTD.TD003 WHERE TE001='D410' AND TE023>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker4.Value.ToShortDateString() + "'),112) AND TE023<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker3.Value.ToShortDateString() + "'),112) AND TE011>0  AND (TA021='" + textBox10.Text + "' OR '" + textBox10.Text + "'='') AND (TA034 LIKE '%" + textBox8.Text + "%' OR '" + textBox8.Text + "'='') AND (TA002 LIKE '%" + textBox4.Text + "%' OR TA083 LIKE '%" + textBox4.Text + "%' OR '" + textBox4.Text + "'='') AND (A.UDF01 LIKE '%" + textBox9.Text + "%' OR '" + textBox9.Text + "'='')  AND (A.UDF06 LIKE '%" + textBox7.Text + "%' OR '" + textBox7.Text + "'='')  AND (MV002 LIKE '%" + textBox11.Text + "%' OR '" + textBox11.Text + "'='')  AND (TE004 = '" + textBox18.Text + "' OR '" + textBox18.Text + "'='') ORDER BY A.TE023,A.TE004";
            //strSql = "SELECT TE023 生产日期,SFCTD.TD003 报工日期,ISNULL((SELECT  TOP 1 B.CREATE_DATE FROM SFCTE B WHERE B.TE006=A.TE006 AND B.TE007=A.TE007 AND B.TE008=A.TE008 AND B.TE001='D420'),'') 派工时间,A.CREATE_DATE 报工时间,ISNULL(SFCTD.UDF01,'') 班次,RTRIM(TE004) 员工编号,RTRIM(MV002) 员工姓名,RTRIM(MV020) 微信账号,TE006 工单单别,TE007 工单单号,RTRIM(TE017) 品号,RTRIM(TE018) 品名,RTRIM(TE019) 规格,RTRIM(MW002) 工艺,RTRIM(A.UDF06) 工艺说明,RTRIM(A.TE015) 备注,ISNULL(COPTD.TD002,'') 订单号,CAST(ISNULL(COPTD.TD008,0) AS REAL) 订单数量,CAST(TE011 AS REAL) 数量,CEILING(A.UDF51) 料废,CEILING(A.UDF52) 工废,CEILING(A.UDF53) 不良,CAST(MGD09 AS real) 计件单价,CAST(TE028 AS real) 报工时单价,CAST(ROUND(TE028*TE011,2) AS REAL) 金额,TA083 项目号,TE001 单别,TE002 单号,TE003 序号,SFCTD.CREATOR 报工账号,M.[工作中心] 部门, M.[生产入库申请单单号] 生产入库申请单单号, M.[生产入库单号] 生产入库单号, CONVERT(DATE, M.[入库时间]) 入库时间, M.[入库数量] 入库数量,M.[合格业务数量] 合格业务数量,M.[不合格业务数量] 不合格业务数量,M.[让步接受量] 让步接受量,M.[破坏数量] 破坏数量 FROM SFCTE A LEFT JOIN SFCTD ON TE001=SFCTD.TD001 AND TE002=SFCTD.TD002 LEFT JOIN INVMB ON TE017=MB001 LEFT JOIN BOMMG ON MB010=MG001 AND MB011=MG002 AND TE008=MG003 AND A.UDF08=MG006 LEFT JOIN CMSMV ON TE004=MV001 LEFT JOIN CMSMW ON TE009=MW001 LEFT JOIN MOCTA ON TE006=TA001 AND TE007=TA002 LEFT JOIN COPTD ON TA026=COPTD.TD001 AND TA027=COPTD.TD002 AND TA028=COPTD.TD003 LEFT JOIN LY_VIEW_001 AS M ON [工单单号] = TE006 + '-' + TE007 AND [入库时间] >= CONVERT(CHAR(8), DATEADD(DAY, 0, '" + dateTimePicker4.Value.ToShortDateString() + "'), 112) AND [入库时间] <=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker3.Value.ToShortDateString() + "'), 112)  AND TE011 > 0 AND [品号] = RTRIM( TE017 ) AND [申请人] = RTRIM( MV002 )" +
            //   " WHERE TE001='D410' AND TE023>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker4.Value.ToShortDateString() + "'),112) AND TE023<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker3.Value.ToShortDateString() + "'),112) AND TE011>0  AND (TA021='" + textBox10.Text + "' OR '" + textBox10.Text + "'='') AND (TA034 LIKE '%" + textBox8.Text + "%' OR '" + textBox8.Text + "'='') AND (TA002 LIKE '%" + textBox4.Text + "%' OR TA083 LIKE '%" + textBox4.Text + "%' OR '" + textBox4.Text + "'='') AND (A.UDF01 LIKE '%" + textBox9.Text + "%' OR '" + textBox9.Text + "'='')  AND (A.UDF06 LIKE '%" + textBox7.Text + "%' OR '" + textBox7.Text + "'='')  AND (MV002 LIKE '%" + textBox11.Text + "%' OR '" + textBox11.Text + "'='')  AND (TE004 = '" + textBox18.Text + "' OR '" + textBox18.Text + "'='') " +          
            //   " ORDER BY A.TE023,A.TE004";
            strSql = "SELECT TE023 生产日期,SFCTD.TD003 报工日期,ISNULL((SELECT  TOP 1 B.CREATE_DATE FROM SFCTE B WHERE B.TE006=A.TE006 AND B.TE007=A.TE007 AND B.TE008=A.TE008 AND B.TE001='D420'),'') 派工时间,A.CREATE_DATE 报工时间,ISNULL(SFCTD.UDF01,'') 班次,RTRIM(TE004) 员工编号,RTRIM(MV002) 员工姓名,RTRIM(MV020) 微信账号,TE006 工单单别,TE007 工单单号,RTRIM(TE017) 品号,RTRIM(TE018) 品名,RTRIM(TE019) 规格,RTRIM(MW002) 工艺,RTRIM(A.UDF06) 工艺说明,RTRIM(A.TE015) 备注,ISNULL(COPTD.TD002,'') 订单号,CAST(ISNULL(COPTD.TD008,0) AS REAL) 订单数量,CAST(TE011 AS REAL) 数量,CEILING(A.UDF51) 料废,CEILING(A.UDF52) 工废,CEILING(A.UDF53) 不良,CAST(MGD09 AS real) 计件单价,CAST(TE028 AS real) 报工时单价,CAST(ROUND(TE028*TE011,2) AS REAL) 金额,TA083 项目号,TE001 单别,TE002 单号,TE003 序号,SFCTD.CREATOR 报工账号,M.[工作中心] 工作中心,MW001 工艺号, M.[生产入库单号] 生产入库单号,M.[入库单序号]  入库序号, CONVERT(DATE, M.[入库时间]) 入库时间, M.[入库数量] 入库数量,M.[合格业务数量] 合格业务数量,M.[不合格业务数量] 不合格业务数量,M.[让步接受量] 让步接受量,M.[破坏数量] 破坏数量 FROM SFCTE A LEFT JOIN SFCTD ON TE001=SFCTD.TD001 AND TE002=SFCTD.TD002 LEFT JOIN INVMB ON TE017=MB001 LEFT JOIN BOMMG ON MB010=MG001 AND MB011=MG002 AND TE008=MG003 AND A.UDF08=MG006 LEFT JOIN CMSMV ON TE004=MV001 LEFT JOIN CMSMW ON TE009=MW001 LEFT JOIN MOCTA ON TE006=TA001 AND TE007=TA002 LEFT JOIN COPTD ON TA026=COPTD.TD001 AND TA027=COPTD.TD002 AND TA028=COPTD.TD003 LEFT JOIN LY_VIEW_001 AS M ON [工单单号] = TE006 + '-' + TE007 AND [入库时间] >= CONVERT(CHAR(8), DATEADD(DAY, 0, '" + dateTimePicker4.Value.ToShortDateString() + "'), 112) AND [入库时间] <= CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker3.Value.ToShortDateString() + "'), 112)  AND TE011 > 0 AND [品号] = RTRIM( TE017 ) " +
               " WHERE TE001='D410' AND TE023>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker4.Value.ToShortDateString() + "'),112) AND TE023<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker3.Value.ToShortDateString() + "'),112) AND TE011>0  AND (TA021='" + textBox10.Text + "' OR '" + textBox10.Text + "'='') AND (TA034 LIKE '%" + textBox8.Text + "%' OR '" + textBox8.Text + "'='') AND (TA002 LIKE '%" + textBox4.Text + "%' OR TA083 LIKE '%" + textBox4.Text + "%' OR '" + textBox4.Text + "'='') AND (A.UDF01 LIKE '%" + textBox9.Text + "%' OR '" + textBox9.Text + "'='')  AND (A.UDF06 LIKE '%" + textBox7.Text + "%' OR '" + textBox7.Text + "'='')  AND (MV002 LIKE '%" + textBox11.Text + "%' OR '" + textBox11.Text + "'='')  AND (TE004 = '" + textBox18.Text + "' OR '" + textBox18.Text + "'='') " +
               " ORDER BY A.TE023,A.TE004";

            //textBox9.Text = strSql;
            //return;
            db = new DataBase();
            try
            {
                dataGridView3.DataSource = db.GetDataSet1(strSql, "SFCTE").Tables["SFCTE"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {
        
        }

        private void textBox6_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strSql = "";
                if(textBox2.Text=="" && textBox5.Text=="")
                { 
                strSql = "SELECT TA001 单别,TA002 单号,RTRIM(TA003) 下单日期,MB001 品号,MB002 品名,MB003 规格,CEILING(TA015) 数量,CEILING(TA017) 已入库数量,TA027 订单号 FROM MOCTA LEFT JOIN COPTC ON TA026=TC001 AND TA027=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TA006=MB001 WHERE (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (MOCTA.TA034 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='') AND TA030='1' AND MOCTA.TA027 LIKE '%" + textBox3.Text + "%'";
                }
                else
                {
                    strSql = "SELECT TA001 单别,TA002 单号,RTRIM(TA003) 下单日期,MB001 品号,MB002 品名,MB003 规格,CEILING(TA015) 数量,CEILING(TA017) 已入库数量,TA027 订单号 FROM MOCTA LEFT JOIN COPTC ON TA026=TC001 AND TA027=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TA006=MB001 WHERE (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (MOCTA.TA034 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='') AND TA030='1' AND MOCTA.TA027 LIKE '%" + textBox3.Text + "%' AND EXISTS(SELECT * FROM BOMMG WHERE MG001=TA006 AND ISNULL(MG007,'') LIKE '%" + textBox2.Text + "%')";
                }
                try
                {
                    DataTable dt;
                    dt = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];
                    gridControl1.DataSource = dt;

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    //throw ex;
                }
            }
        }

        private void textBox3_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strSql = "";
                if (textBox2.Text == "" && textBox5.Text == "")
                {
                    strSql = "SELECT TA001 单别,TA002 单号,RTRIM(TA003) 下单日期,MB001 品号,MB002 品名,MB003 规格,CEILING(TA015) 数量,CEILING(TA017) 已入库数量,TA027 订单号 FROM MOCTA LEFT JOIN COPTC ON TA026=TC001 AND TA027=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TA006=MB001 WHERE (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (MOCTA.TA034 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='') AND TA030='1' AND MOCTA.TA033 LIKE '%" + textBox3.Text + "%'";
                }
                else
                {
                    strSql = "SELECT TA001 单别,TA002 单号,RTRIM(TA003) 下单日期,MB001 品号,MB002 品名,MB003 规格,CEILING(TA015) 数量,CEILING(TA017) 已入库数量,TA027 订单号 FROM MOCTA LEFT JOIN COPTC ON TA026=TC001 AND TA027=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMB ON TA006=MB001 WHERE (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (MOCTA.TA034 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='') AND TA030='1' AND MOCTA.TA033 LIKE '%" + textBox3.Text + "%' AND EXISTS(SELECT * FROM BOMMG WHERE MG001=TA006 AND ISNULL(MG007,'') LIKE '%" + textBox2.Text + "%')";
                }
                try
                {
                    DataTable dt;
                    dt = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];
                    gridControl1.DataSource = dt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    //throw ex;
                }
            }
        }

        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                String x1 = "";
                String x2 = "";
                Double x3 = 0.00;
                String x4 = "";
                String x5 = "";
                Double x6 = 0.00;
                Double x7 = 0.00;
                Double x8 = 0.00;
                double ybsl = (dataGridView2["YBSL", dataGridView2.CurrentCell.RowIndex].Value == null) ? 0.00 : Convert.ToDouble(dataGridView2["YBSL", dataGridView2.CurrentCell.RowIndex].Value);
                x1 = dataGridView2["TA001", dataGridView2.CurrentCell.RowIndex].Value.ToString();
                x2 = dataGridView2["TA002", dataGridView2.CurrentCell.RowIndex].Value.ToString();
                x3 = Convert.ToDouble(dataGridView2["HGSL", dataGridView2.CurrentCell.RowIndex].Value.ToString());
                x5 = dataGridView2["XH", dataGridView2.CurrentCell.RowIndex].Value.ToString();
                x6 = Convert.ToDouble(dataGridView2["GFSL", dataGridView2.CurrentCell.RowIndex].Value.ToString());
                x7 = Convert.ToDouble(dataGridView2["LFSL", dataGridView2.CurrentCell.RowIndex].Value.ToString());
                x8 = Convert.ToDouble(dataGridView2["BLSL", dataGridView2.CurrentCell.RowIndex].Value.ToString());
                BGMX xx = new BGMX(x1, x2, x3, x4, x5,x6,x7,x8);
                //MessageBox.Show( "1111", "软件提示");
                form1.cdgzzx += new GXBGDItem.Cdgzzx(textBox2_TextChanged);
                form1.Show(this);
            }
            catch
            {

            }
        }

        private void j_DataSelected(object sender, EventArgs e)
        {
            String User = "";
            String Name = "";
            Double OKQTY = 0.00;
            Double NGQTY = 0.00;
            Double MreQTY = 0.00;
            Double PreQTY = 0.00;

            dt = form1.Returndt;
            for (int i = 0; i < form1.UserIds.Count; i++)
            {
                if (User == "")
                {
                    User = form1.UserIds[i].ToString();
                    Name = form1.UserNames[i].ToString();
                    OKQTY = form1.OKQTYS[i];
                    NGQTY = form1.NGQTYS[i];
                    MreQTY = form1.MreQTYS[i];
                    PreQTY = form1.PreQTYS[i];
                }
                else
                {
                    User +=","+ form1.UserIds[i].ToString();
                    Name +=","+ form1.UserNames[i].ToString();
                    OKQTY += form1.OKQTYS[i];
                    NGQTY += form1.NGQTYS[i];
                    MreQTY += form1.MreQTYS[i];
                    PreQTY += form1.PreQTYS[i];
                }
            }
            dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells["YGBH"].Value = User;
            dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells["YGXM"].Value = Name;
            dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells["HGSL"].Value = OKQTY.ToString();
            dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells["BLSL"].Value = NGQTY.ToString();
            dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells["GFSL"].Value = PreQTY.ToString();
            dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells["LFSL"].Value = MreQTY.ToString();

            dataGridView2.ClearSelection();
            dataGridView2.CurrentCell = dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells["HGSL"];

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void GXBGD_FormClosing(object sender, FormClosingEventArgs e)
        {
            form1.Dispose();
            form1.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            gridControl1.MainView = gridView2;
            //dataGridView2.Rows.Clear();
            //form1.Dispose();
            //form1.Close();
            //form1 = new GXBGDItem();
            //form1.DataSelected += new EventHandler(j_DataSelected);
            //form1.Hide();
        }

        //录数窗口
        //public DataSet ds_jj(string hth, string dh2)

        //{

            //DataSet ds = new DataSet();//

            //DataSet ds_scgjb = new DataSet();//主表

            //DataSet ds_jjmx = new DataSet();//从表



            //ds_scgjb = dal_scgj.GetItemsByHTDH(hth, dh2);

            //ds_jjmx = dal_jjmx.GetItemsByHTDH(hth, dh2);


            //ds.Tables.Add(ds_scgjb.Tables[0].Copy());

            //ds.Tables.Add(ds_jjmx.Tables[0].Copy());

            ////主从字表约束字段：这里有三个约束字段

            //DataColumn[] column3 = new DataColumn[] {
            //    ds.Tables["jj_gjb"].Columns["合同号"], ds.Tables["jj_gjb"].Columns["袋号"], ds.Tables["jj_gjb"].Columns["工序代码"] };


            // DataColumn[] column4 = new DataColumn[] {
            //        ds.Tables["jj_jjmx"].Columns["合同号"], ds.Tables["jj_jjmx"].Columns["袋号"], ds.Tables["jj_jjmx"].Columns["工序代码"] };

            //         //建立约束名
            //        if (ds.Relations.IndexOf("jjgz") < 0)

            //        {
            //            ds.Relations.Add("jjgz", column3, column4);

            //        }

            //        return ds;

                //}

                private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void dataGridView2_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void dataGridView3_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataGridView2.Rows.RemoveAt(dataGridView2.CurrentRow.Index);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox1.CheckState==CheckState.Checked)
            {
                button2.Text = "派工";
                dataGridView2.Columns["YJCL"].HeaderText = "可派数量";
                dataGridView2.Columns["YBSL"].HeaderText = "已派数量";
                dataGridView2.Columns["WBSL"].HeaderText = "未派数量";
                dataGridView2.Columns["HGSL"].HeaderText = "派工数量";
                dataGridView2.Columns["GFSL"].Visible = false;
                dataGridView2.Columns["LFSL"].Visible = false;
                dataGridView2.Columns["BLSL"].Visible = false;
                checkBox4.CheckState = CheckState.Unchecked;
            }
            else
            {
                button2.Text = "报工";
                dataGridView2.Columns["YJCL"].HeaderText = "可报数量";
                dataGridView2.Columns["YBSL"].HeaderText = "已报数量";
                dataGridView2.Columns["WBSL"].HeaderText = "未报数量";
                dataGridView2.Columns["HGSL"].HeaderText = "合格数量";
                dataGridView2.Columns["GFSL"].Visible = true;
                dataGridView2.Columns["LFSL"].Visible = true;
                dataGridView2.Columns["BLSL"].Visible = true;
                checkBox4.CheckState = CheckState.Checked;
            }
        }

        private void textBox12_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Control || e.KeyCode == Keys.Enter)
            {
                checkBox1.CheckState = CheckState.Unchecked;
                checkBox4.CheckState = CheckState.Checked;
                button2.Text = "报工";
                dataGridView2.Columns["YJCL"].HeaderText = "可报数量";
                dataGridView2.Columns["YBSL"].HeaderText = "已报数量";
                dataGridView2.Columns["WBSL"].HeaderText = "未报数量";
                dataGridView2.Columns["HGSL"].HeaderText = "合格数量";
                dataGridView2.Columns["GFSL"].Visible = true;
                dataGridView2.Columns["LFSL"].Visible = true;
                dataGridView2.Columns["BLSL"].Visible = true;

                dataGridView2.Rows.Clear();
                string strSql = null;
                //strSql = "SELECT TE003,TE006,TE007,TE008,RTRIM(ISNULL(A.UDF08,'')),RTRIM(TE018),RTRIM(ISNULL(A.UDF01,'')),RTRIM(ISNULL(A.UDF06,'')) MG007,CEILING(A.UDF57) AS TA015,CAST((SELECT SUM(TE011) FROM SFCTE B WHERE TE001='D410' AND B.UDF09=A.TE001 AND B.UDF10=A.TE002 AND B.UDF11=A.TE003) AS REAL),CEILING(A.UDF57)-CAST((SELECT SUM(TE011) FROM SFCTE B WHERE TE001='D410' AND B.UDF09=A.TE001 AND B.UDF10=A.TE002 AND B.UDF11=A.TE003) AS REAL) WBSL,0 HGSL,0 BLSL,0 GFSL,0 LFSL,RTRIM(TE004) YGBH,RTRIM(MV002) YGXM,'' ZRGX,RTRIM(TE015) BZ,TE001,TE002,TE028 AS JJDJ FROM SFCTE A LEFT JOIN CMSMV ON TE004=MV001 WHERE  TE001+'-'+TE002=SUBSTRING('" + textBox12.Text + "',1,16) ORDER BY TE003";
                strSql = "SELECT TE003,TE006,TE007,TE008,RTRIM(ISNULL(A.UDF08,'')),RTRIM(TE018),RTRIM(ISNULL(A.UDF01,'')),RTRIM(ISNULL(A.UDF06,'')) MG007,CEILING(A.UDF57) AS TA015,CAST((SELECT YBSL FROM V_SFCTE B WHERE B.TE006=A.TE006 AND B.TE007=A.TE007 AND B.TE008=A.TE008) AS REAL),CEILING(A.UDF57)-CAST((SELECT YBSL FROM V_SFCTE B WHERE B.TE006=A.TE006 AND B.TE007=A.TE007 AND B.TE008=A.TE008) AS REAL) WBSL,0 HGSL,0 BLSL,0 GFSL,0 LFSL,RTRIM(TE004) YGBH,RTRIM(MV002) YGXM,'' ZRGX,RTRIM(TE015) BZ,TE001,TE002,TE028 AS JJDJ FROM SFCTE A LEFT JOIN CMSMV ON TE004=MV001 WHERE  TE001+'-'+TE002=SUBSTRING('" + textBox12.Text + "',1,16) ORDER BY TE003";

                DataTable dt;
                //textBox2.Text = strSql;
                //try
                //{ 
                db = new DataBase();
                dt = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    int index = this.dataGridView2.Rows.Add();
                    //MessageBox.Show(index.ToString(),"1");
                    this.dataGridView2.Rows[index].Cells[0].Value = false; 
                    String XH = "000" + (index + 1).ToString();
                    XH = XH.Substring(XH.Length - 4, 4);
                    this.dataGridView2.Rows[index].Cells[1].Value = XH;
                    this.dataGridView2.Rows[index].Cells[2].Value = dt.Rows[i][1].ToString();
                    this.dataGridView2.Rows[index].Cells[3].Value = dt.Rows[i][2].ToString();
                    this.dataGridView2.Rows[index].Cells[4].Value = dt.Rows[i][3].ToString();
                    this.dataGridView2.Rows[index].Cells[5].Value = dt.Rows[i][4].ToString();
                    this.dataGridView2.Rows[index].Cells[6].Value = dt.Rows[i][5].ToString();
                    this.dataGridView2.Rows[index].Cells[7].Value = dt.Rows[i][6].ToString();
                    this.dataGridView2.Rows[index].Cells[8].Value = dt.Rows[i][7].ToString();
                    this.dataGridView2.Rows[index].Cells[9].Value = dt.Rows[i][8].ToString();
                    this.dataGridView2.Rows[index].Cells[10].Value = dt.Rows[i][9].ToString();
                    this.dataGridView2.Rows[index].Cells[11].Value = dt.Rows[i][10].ToString();
                    this.dataGridView2.Rows[index].Cells[12].Value = dt.Rows[i][11].ToString();
                    this.dataGridView2.Rows[index].Cells[13].Value = dt.Rows[i][12].ToString();
                    this.dataGridView2.Rows[index].Cells[14].Value = dt.Rows[i][13].ToString();
                    this.dataGridView2.Rows[index].Cells[15].Value = dt.Rows[i][14].ToString();
                    this.dataGridView2.Rows[index].Cells[16].Value = dt.Rows[i][15].ToString();
                    this.dataGridView2.Rows[index].Cells[17].Value = dt.Rows[i][16].ToString();
                    this.dataGridView2.Rows[index].Cells[18].Value = dt.Rows[i][17].ToString();
                    this.dataGridView2.Rows[index].Cells[19].Value = dt.Rows[i][18].ToString();
                    this.dataGridView2.Rows[index].Cells[20].Value = dt.Rows[i][19].ToString();
                    this.dataGridView2.Rows[index].Cells[21].Value = dt.Rows[i][20].ToString();
                    this.dataGridView2.Rows[index].Cells[22].Value = dt.Rows[i][21].ToString();
                    textBox12.Text = "";
                    }

          //      }           
          //catch
          //  {

          //  }
        }
     }

        private void button6_Click(object sender, EventArgs e)
        {
            DesignReport();
        }
        private void DesignReport()
        {
            if (FReportRunning)
                return;
            string thisFolder = Config.ApplicationFolder;
            DirectoryInfo theFolder = new DirectoryInfo(thisFolder + @"Reports\");
            string ReportName = theFolder + "派工卡.frx";
            FReport = new Report();
            FReport.Load(ReportName);
            RegisterData();
            FReport.Design();
            //PreviewReport();
        }

        private void RegisterData()
        {
            //FReport.RegisterData(FDataSet, "NorthWind");
            //FReport.RegisterData(FBusinessObject, "Categories BusinessObject");
        }

        private void PreviewReport()
        {
            if (FReportRunning)
                return;

            FReportRunning = true;
            try
            {
                RegisterData();
                FReport.Show();
            }
            finally
            {
                FReportRunning = false;
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {

            string strSql = null;
            if(checkBox2.CheckState==CheckState.Checked)
            {
            strSql = "SELECT TD003 派工日期,RTRIM(TE004) 员工编号,RTRIM(MV002) 员工姓名,TE006 工单单别,TE007 工单单号,RTRIM(TE017) 品号,RTRIM(TE018) 品名,RTRIM(TE019) 规格,RTRIM(ISNULL(A.UDF06,CMSMW.MW002)) 工艺说明,RTRIM(A.TE015) 备注,CEILING(A.UDF57) 数量,A.TE001 单别,A.TE002 单号,A.TE003 序号,SFCTD.CREATOR 派工账号 FROM SFCTE A LEFT JOIN SFCTD ON TE001=TD001 AND TE002=TD002 LEFT JOIN CMSMV ON TE004=MV001 LEFT JOIN CMSMW ON TE009=MW001 WHERE TD001='D420' AND TD003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND TD003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker5.Value.ToShortDateString() + "'),112) AND (TE018 LIKE '%" + textBox15.Text + "%' OR '" + textBox15.Text + "'='') AND (MV002 LIKE '%" + textBox16.Text + "%' OR TE004='" + textBox16.Text + "' OR '" + textBox16.Text + "'='') AND (A.UDF06='" + textBox14.Text + "' OR '" + textBox14.Text + "'='') AND (A.TE002='" + textBox13.Text + "' OR A.TE007='" + textBox13.Text + "' OR '" + textBox13.Text + "'='') ORDER BY TD003,A.TE002,A.TE003";
            }
            else
            {
            strSql = "SELECT TD003 派工日期,RTRIM(TE004) 员工编号,RTRIM(MV002) 员工姓名,TE006 工单单别,TE007 工单单号,RTRIM(TE017) 品号,RTRIM(TE018) 品名,RTRIM(TE019) 规格,RTRIM(ISNULL(A.UDF06,CMSMW.MW002)) 工艺说明,RTRIM(A.TE015) 备注,CEILING(A.UDF57) 数量,A.TE001 单别,A.TE002 单号,A.TE003 序号,SFCTD.CREATOR 派工账号 FROM SFCTE A LEFT JOIN SFCTD ON TE001=TD001 AND TE002=TD002 LEFT JOIN CMSMV ON TE004=MV001 LEFT JOIN CMSMW ON TE009=MW001 WHERE TD001='D420' AND TD003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND TD003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker5.Value.ToShortDateString() + "'),112) AND (TE018 LIKE '%" + textBox15.Text + "%' OR '" + textBox15.Text + "'='') AND (MV002 LIKE '%" + textBox16.Text + "%' OR TE004='" + textBox16.Text + "' OR '" + textBox16.Text + "'='') AND (A.UDF06='" + textBox14.Text + "' OR '" + textBox14.Text + "'='') AND (A.TE002='" + textBox13.Text + "' OR A.TE007='" + textBox13.Text + "' OR '" + textBox13.Text + "'='') AND SFCTD.CREATOR='" + PropertyClass.OperatorCode + "' ORDER BY TD003,A.TE002,A.TE003";
            }

            db = new DataBase();
            try
            {
               gridControl2.DataSource = db.GetDataSet1(strSql, "SFCTE").Tables["SFCTE"];

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                string bgdb = (gridView2.GetFocusedRowCellValue("单别") == null) ? "" : gridView2.GetFocusedRowCellValue("单别").ToString();
                string bgdh = (gridView2.GetFocusedRowCellValue("单号") == null) ? "" : gridView2.GetFocusedRowCellValue("单号").ToString();
                string bgxh = (gridView2.GetFocusedRowCellValue("序号") == null) ? "" : gridView2.GetFocusedRowCellValue("序号").ToString();
                if (bgdh == "")
                {
                    MessageBox.Show("没有选择补打哪一行！", "系统提示");
                    return;
                }

                string strSql = "";
                strSql = "UPDATE SFCTD SET TD014='补打' WHERE TD001='" + bgdb + "' AND TD002='" + bgdh + "'";
                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {

                    }
                    else
                    {
                        MessageBox.Show("补打失败，请检查！", "软件提示");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
            }
            catch
            {
                MessageBox.Show("没有选择补打哪一行！", "系统提示");
                return;
            }
            string x1 = gridView2.GetFocusedRowCellValue("单别").ToString();
            string x2 = gridView2.GetFocusedRowCellValue("单号").ToString();
            string x3 = gridView2.GetFocusedRowCellValue("序号").ToString();
            SJCD xx = new SJCD(x1, x2, x3);
            LabelPrint L = new LabelPrint();
            L.cdssh = new LabelPrint.Cdssh(textBox2_TextChanged);
            L.Show();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            try
            {
                int i = 0;
                foreach (DataGridViewRow dr in this.dataGridView5.Rows)
                {
                    string s = (dr.Cells["YGBH2"].Value == null) ? "" : dr.Cells["YGBH2"].Value.ToString();
                    if (s != "")
                    {
                        i++;
                    }
                    string scrq = (dr.Cells["ZYRQ"].Value == null) ? "" : dr.Cells["ZYRQ"].Value.ToString();

                    if (s != "" && (scrq == "0" || scrq == ""))
                    {
                        MessageBox.Show("生产日期不能为空！", "软件提示");
                        return;
                    }
                    try
                    {
                        int scrcqcd = Convert.ToInt32(scrq.Trim().Length.ToString());

                        if (s != "" && (scrcqcd != 8) || Convert.ToInt32(scrq.Trim().Substring(0, 4)) < 2019 || Convert.ToInt32(scrq.Trim().Substring(0, 4)) >= 2029)
                        {
                            MessageBox.Show("生产日期必须为完整的8位数日期，且合理！", "软件提示");
                            return;
                        }
                    }
                    catch
                    {

                    }


                }
                if (i == 0)
                {
                    MessageBox.Show("员工编号未填！", "软件提示");
                    return;
                }

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
                //throw ex;
            }
            finally
            {

            }



            string strSql = null;
            string code = null;
            strSql = "SELECT ISNULL(CAST(CAST(MAX(TD002) AS NUMERIC(11))+1 AS CHAR(11)) ,CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker6.Value.ToShortDateString() + "'),112)+'001') AS DH FROM SFCTD WHERE TD001='D430' AND TD002 LIKE CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker6.Value.ToShortDateString() + "'),112) + '%'";
            //MessageBox.Show(strSql, "提示");
            try
            {
                sdr1 = db.GetDataReader1(strSql);

                while (sdr1.Read())
                {
                    code = sdr1["DH"].ToString();
                }

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }
            finally
            {
                sdr1.Close();
            }
            strSql = "INSERT INTO SFCTD(CREATOR,FLAG,TD001,TD002,TD003,TD004,TD005,TD006,TD007,TD008,TD009,TD010,TD011,TD012,TD013,TD014,TD015,TD016,TD017,TD018,TD019,TD020)  SELECT '" + PropertyClass.OperatorCode + "',1,'D430','" + code + "',CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker6.Value.ToShortDateString() + "'),112),'" + textBox17.Text + "','N','',0,CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker6.Value.ToShortDateString() + "'),112),'','N',0,'','','',0,0,0,'**********',0,'********************'";
            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {

                }
                else
                {
                    MessageBox.Show("生成报工单头失败，请检查" + code, "软件提示");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
            foreach (DataGridViewRow dr in this.dataGridView5.Rows)
            {
                string s = (dr.Cells["YGBH2"].Value == null) ? "" : dr.Cells["YGBH2"].Value.ToString();
                if (s != "")
                {
                    strSql = "INSERT INTO SFCTE(CREATE_DATE,FLAG,TE001,TE002,TE003,TE004,TE005,TE006,TE007,TE008,TE009,TE010,TE011,TE012,TE013,TE014,TE015,TE016,TE017,TE018,TE019,TE020,TE021,TE022,TE023,TE024,TE025,TE026,TE027,TE028,TE029,TE030,TE031,TE032,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF07,UDF57,UDF58,UDF59,UDF08,UDF10) SELECT REPLACE(REPLACE(REPLACE(CONVERT(CHAR(20),GETDATE(),120),'-',''),':',''),' ',''),1,TD001,TD002,RIGHT(RTRIM('000'+CAST(MAX(ISNULL(TE003,0))+1 AS CHAR(4))),4),'" + dr.Cells["YGBH2"].Value + "','','8101','20200107001','0010','Q00','1',0,0,0,'N','" + dr.Cells["BZ2"].Value + "',0,'10000','点工专用品号','','','','','" + dr.Cells["ZYRQ"].Value + "','',0,0,0,0,0,'1',0,'********************',0,0,";
                    strSql = strSql + "0,0,0," + dr.Cells["JGGS2"].Value + ",'" + dr.Cells["QGLX"].Value + "','','','','" + code + "','','" + dr.Cells["YGXM2"].Value + "',0,0,0,'','' FROM SFCTD LEFT JOIN SFCTE ON TE001=TD001 AND TE002=TD002  WHERE TD001='D430' AND TD002='" + code + "' GROUP BY TD001,TD002";

                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {

                        }
                        else
                        {
                            MessageBox.Show("生成报工单身失败，请检查" + code, "软件提示");
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }

                    string fqr = "";
                    //员工姓名
                    string ygxm = "";
                    strSql = "SELECT MV002,MV020 FROM CMSMV WHERE MV001='" + dr.Cells["YGBH2"].Value + "'";
                    //MessageBox.Show(strSql,"提示");
                    try
                    {
                        sdr1 = db.GetDataReader1(strSql);

                        while (sdr1.Read())
                        {
                            fqr = sdr1["MV020"].ToString();
                            ygxm = sdr1["MV002"].ToString();
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        //throw ex;
                    }
                    finally
                    {
                        sdr1.Close();
                    }
                    if (fqr != "")
                    {
                        var client = new RestClient("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww9f0e8d7105c6b0e7&corpsecret=C8B6Y3-N2JsuYWR2iRAX-s-Bcnmc6Mxdi-ChhBxyjJY");
                        client.Timeout = -1;
                        var request = new RestRequest(Method.GET);
                        IRestResponse response = client.Execute(request);
                        Logininfor lg = JsonConvert.DeserializeObject<Logininfor>(response.Content);
                        String tokenstring = lg.access_token.ToString();

                        client = new RestClient("https://qyapi.weixin.qq.com/cgi-bin/oa/applyevent?access_token=" + tokenstring + "");
                        client.Timeout = -1;
                        request = new RestRequest(Method.POST);
                        request.AddHeader("Content-Type", "text/plain");
                        request.AddParameter("text/plain", "{\r\n    \"creator_userid\": \"" + fqr + "\",\r\n    \"template_id\": \"3TmkoTz9ScKRiwWoXFeyWLho2NkL6ytyB1A5WYLg\",\r\n    \"use_template_approver\":1,\r\n    \"apply_data\": {\r\n         \"contents\": [\r\n                {\r\n                    \"control\": \"Text\",\r\n                    \"id\": \"Text-1606801528382\",\r\n                    \"value\": {\r\n                        \"text\": \"" + ygxm + "\"\r\n                    }\r\n                },\r\n                {\r\n                    \"control\": \"Text\",\r\n                    \"id\": \"Text-1606801560227\",\r\n                    \"value\": {\r\n                        \"text\": \"" + dr.Cells["BZ2"].Value + "\"\r\n                    }\r\n                },\r\n                {\r\n                    \"control\": \"Text\",\r\n                    \"id\": \"Text-1606801593259\",\r\n                    \"value\": {\r\n                        \"text\": \"" + dr.Cells["ZYRQ"].Value + "\"\r\n                    }\r\n                },\r\n                {\r\n                    \"control\": \"Text\",\r\n                    \"id\": \"Text-1606801632438\",\r\n                    \"value\": {\r\n                        \"text\": \"" + dr.Cells["QGLX"].Value + "\"\r\n                    }\r\n                },\r\n                {\r\n                    \"control\": \"Number\",\r\n                    \"id\": \"Number-1606804954419\",\r\n                    \"value\": {\r\n                        \"new_number\": \"" + dr.Cells["JGGS2"].Value + "\"\r\n                    }\r\n                }\r\n            ]\r\n    },\r\n    \"summary_list\": [\r\n        {\r\n            \"summary_info\": [{\r\n                \"text\": \"\",\r\n                \"lang\": \"zh_CN\"\r\n            }]\r\n        },\r\n        {\r\n            \"summary_info\": [{\r\n                \"text\": \"\",\r\n                \"lang\": \"zh_CN\"\r\n            }]\r\n        },\r\n        {\r\n            \"summary_info\": [{\r\n                \"text\": \"" + dr.Cells["QGLX"].Value + "\",\r\n                \"lang\": \"zh_CN\"\r\n            }]\r\n        }\r\n    ]\r\n}", ParameterType.RequestBody);
                        response = client.Execute(request);
                        Flowinfor Fl = JsonConvert.DeserializeObject<Flowinfor>(response.Content);
                        String Sp_NO = Fl.sp_no.ToString();
                        strSql = "UPDATE SFCTD SET TD006=RTRIM(TD006)+'" + Sp_NO + "' WHERE TD001='D430' AND TD002='" + code + "'";
                        //textBox18.Text = strSql;
                        try
                        {
                            if (db.ExecDataBySql1(strSql) > 0)
                            {
                            }
                            else
                            {
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            return;
                        }
                        MessageBox.Show("微信审批单号：" + Sp_NO, "系统提示");
                    }
                }
            }
            //MessageBox.Show("报工成功！报工单号" + code, "软件提示");

            textBox17.Text = "";
            //DataTable dt = (DataTable)dataGridView2.DataSource;
            //dt.Rows.Clear();
            //dataGridView2.DataSource = dt;
            dataGridView5.Rows.Clear();
        }

        private void dataGridView5_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            this.dataGridView5.Controls.Clear();//移除所有控件 
            if (e.ColumnIndex.Equals(this.dataGridView5.Columns["YGBH2"].Index))
            //if (e.ColumnIndex == 13)
            //判断单元格是否是"Company"列？    
            {
                System.Windows.Forms.Button btn = new System.Windows.Forms.Button();//创建Buttonbtn    
                btn.Text = "...";//设置button文字    
                btn.Font = new System.Drawing.Font("Arial", 5);//设置文字格式    
                btn.Visible = true;//设置控件允许显示    

                btn.Width = this.dataGridView5.GetCellDisplayRectangle(e.ColumnIndex,
                                e.RowIndex, true).Height;//获取单元格高并设置为btn的宽    
                btn.Height = this.dataGridView5.GetCellDisplayRectangle(e.ColumnIndex,
                                e.RowIndex, true).Height;//获取单元格高并设置为btn的高    

                btn.Click += new EventHandler(btnC_Click);//为btn添加单击事件    

                this.dataGridView5.Controls.Add(btn);//dataGridView1中添加控件btn    

                btn.Location = new System.Drawing.Point(((this.dataGridView5.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Right) -
                        (btn.Width)), this.dataGridView5.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y);//设置btn显示位置    
            }
        }

        void btnC_Click(object sender, EventArgs e)
        {
            //string x1 = dataGridView1["工作中心", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            //SCGY xx = new SCGY(x1);
            z = new DDKC();
            z.kclx = "yuangong";
            z.DataSelected += new EventHandler(z_DataSelected);
            //f.cdgzzx += new SCKC.Cdgzzx(textBox2_TextChanged);
            z.Show(this);
        }
        void z_DataSelected(object sender, EventArgs e)
        {
      
            dataGridView5.Rows[dataGridView5.CurrentRow.Index].Cells["YGBH2"].Value = z.UserId.ToString();
            dataGridView5.Rows[dataGridView5.CurrentRow.Index].Cells["YGXM2"].Value = z.UserName.ToString();
        }

        private void dataGridView5_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string s = (this.dataGridView5.CurrentCell.Value == null) ? "" : this.dataGridView5.CurrentCell.Value.ToString();
            if (s != "")
            {
                if (e.ColumnIndex.Equals(this.dataGridView5.Columns["YGBH2"].Index))
                {
                    string strSql = null;

                    strSql = "SELECT MV002 FROM CMSMV WHERE MV001='" + this.dataGridView5.CurrentCell.Value + "'";

                    //MessageBox.Show(strSql,"提示");
                    try
                    {
                        sdr1 = db.GetDataReader1(strSql);

                        while (sdr1.Read())
                        {
                            this.dataGridView5.Rows[e.RowIndex].Cells[this.dataGridView5.Columns["YGXM2"].Index].Value = sdr1["MV002"].ToString();

                        }

                        if (sdr1.HasRows == false)
                        {

                            MessageBox.Show("该员工编号不存在！", "软件提示");

                            this.dataGridView5.CurrentCell.Value = "";
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        //throw ex;
                    }
                    finally
                    {
                        sdr1.Close();
                    }
                }


            }

            if (s == "")
            {
                if (e.ColumnIndex.Equals(this.dataGridView5.Columns["YGBH2"].Index))
                {
                    this.dataGridView5.Rows[e.RowIndex].Cells[this.dataGridView5.Columns["YGXM2"].Index].Value = "";
                    this.dataGridView5.Rows[e.RowIndex].Cells[this.dataGridView5.Columns["YGBH2"].Index].Value = "";
                }
            }

        }

        private void 删除派工记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int[] rows = this.gridView2.GetSelectedRows();
            foreach (int row in rows)
            {
                String pgzh = this.gridView2.GetRowCellValue(row, "派工账号").ToString().Trim();
                String pgdh = this.gridView2.GetRowCellValue(row, "单号").ToString().Trim();
                if (PropertyClass.OperatorCode.Trim() != pgzh)
                {
                    MessageBox.Show(pgdh + "这张派工单不是您派的，您无权删除，请联系派单人员!派工账号：" + gridView2.GetFocusedRowCellValue("派工账号").ToString(), "系统提示" + PropertyClass.OperatorCode.Trim());
                    return;
                }
            }


            foreach (int row in rows)
            {
                String pgdb = this.gridView2.GetRowCellValue(row, "单别").ToString().Trim();
                String pgdh = this.gridView2.GetRowCellValue(row, "单号").ToString().Trim();
                //先删除
                string strSql = "DELETE FROM SFCTE WHERE TE001='" + pgdb + "' AND TE002='" + pgdh + "'";
                if (db.ExecDataBySql1(strSql) > 0)
                {
                    GridControl grid = gridControl2 as GridControl;
                    GridView view = grid.FocusedView as GridView;
                    view.DeleteSelectedRows();                 
                }          
            }
            button7.PerformClick();
            MessageBox.Show("删除成功!", "系统提示");
        }

        private void contextMenuStrip2_Opening(object sender, CancelEventArgs e)
        {

        }

        private void 删除报工记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (PropertyClass.OperatorCode.Trim() != dataGridView3.CurrentRow.Cells["报工账号"].Value.ToString().Trim())
            {
                MessageBox.Show("这张派工单不是您报的，您无权删除，请联系报工人员!报工账号："+ dataGridView3.CurrentRow.Cells["报工账号"].Value.ToString().Trim(), "系统提示");
                return;
            }
            //先删除
            string strSql = "DELETE FROM SFCTE WHERE TE001='" + dataGridView3.CurrentRow.Cells["单别"].Value.ToString() + "' AND TE002='" + dataGridView3.CurrentRow.Cells["单号"].Value.ToString() + "' AND TE003='" + dataGridView3.CurrentRow.Cells["序号"].Value.ToString() + "'";
            if (db.ExecDataBySql1(strSql) > 0)
            {
                dataGridView3.Rows.RemoveAt(dataGridView3.CurrentRow.Index);
                button4.PerformClick();
                MessageBox.Show("删除成功!", "系统提示");
            }
        }

        private void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {

        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            this.dataGridView2.Rows.Clear();
            try
            {
                string strSql = "";
                if (gridView1.GetFocusedRowCellValue("工作中心").ToString().Trim() == "1008" || gridView1.GetFocusedRowCellValue("工作中心").ToString().Trim() == "1007")
                {
                    if (checkBox1.CheckState == CheckState.Checked)
                    {
                        strSql = "SELECT RIGHT('0000'+RTRIM(CAST(ROW_NUMBER()OVER(ORDER BY MF003,MG003) AS CHAR(4))),4),B.TA001,B.TA002,MF003 AS TA003,ISNULL(MG006,'') MG006,TA034,C.MW002,MF008 AS MG007,CAST(TA015*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402) AS REAL) TA015,ISNULL(CEILING(YPSL),0),CAST(TA015*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402)-ISNULL(CEILING(YPSL),0) AS REAL) WBSL,0 HGSL,0 BLSL,0 GFSL,0 LFSL,'' YGBH,'' YGXM,'' ZRGX,'' BZ,TA001 AS TE001,TA002 AS TE002,CAST(ISNULL(MGD09,0) AS REAL) AS JJDJ,ISNULL(MF023,'') 下阶品号,ISNULL(F.MB002,'') 下阶品名,ISNULL(F.MB029,'') 下阶图号,CONVERT(CHAR(10),DATEADD(DAY,CEILING(ISNULL(MF009,0)/8),GETDATE()),111) 要求完工日期 FROM MOCTA B LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN BOMMG ON MF001=MG001 AND MF002=MG002 AND MF003=MG003 LEFT JOIN CMSMW C ON C.MW001=MF004 LEFT JOIN CMSMW D ON D.MW001=MGD01 LEFT JOIN V_SFCTE ON B.TA001=TE006 AND B.TA002=TE007 AND MF003=TE008 LEFT JOIN ITEM E ON E.ITEM_CODE=MF023 LEFT JOIN INVMB F ON MF023=F.MB001 WHERE  B.TA001='" + gridView1.GetFocusedRowCellValue("单别").ToString() + "' AND B.TA002='" + gridView1.GetFocusedRowCellValue("单号").ToString() + "' AND ISNULL(D.MW002,'') LIKE '%" + textBox2.Text + "%' AND ISNULL(MF003,'')='" + gridView1.GetFocusedRowCellValue("工序号").ToString().Trim() + "' ORDER BY MF003,MG006";
                    }
                    else
                    {
                        strSql = "SELECT RIGHT('0000'+RTRIM(CAST(ROW_NUMBER()OVER(ORDER BY MF003,MG003) AS CHAR(4))),4),B.TA001,B.TA002,MF003 AS TA003,ISNULL(MG006,'') MG006,TA034,C.MW002,MF008 AS MG007,CAST(TA015*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402) AS REAL) TA015,ISNULL(CEILING(YBSL),0),CAST(TA015*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402)-ISNULL(CEILING(YBSL),0) AS REAL) WBSL,0 HGSL,0 BLSL,0 GFSL,0 LFSL,'' YGBH,'' YGXM,'' ZRGX,'' BZ,TA001 AS TE001,TA002 AS TE002,CAST(ISNULL(MGD09,0) AS REAL) AS JJDJ,ISNULL(MF023,'') 下阶品号,ISNULL(F.MB002,'') 下阶品名,ISNULL(F.MB029,'') 下阶图号,CONVERT(CHAR(10),DATEADD(DAY,CEILING(ISNULL(MF009,0)/8),GETDATE()),111) 要求完工日期 FROM MOCTA B LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN BOMMG ON MF001=MG001 AND MF002=MG002 AND MF003=MG003 LEFT JOIN CMSMW C ON C.MW001=MF004 LEFT JOIN CMSMW D ON D.MW001=MGD01 LEFT JOIN V_SFCTE ON B.TA001=TE006 AND B.TA002=TE007 AND MF003=TE008 LEFT JOIN ITEM E ON E.ITEM_CODE=MF023 LEFT JOIN INVMB F ON MF023=F.MB001 WHERE  B.TA001='" + gridView1.GetFocusedRowCellValue("单别").ToString() + "' AND B.TA002='" + gridView1.GetFocusedRowCellValue("单号").ToString() + "' AND ISNULL(D.MW002,'') LIKE '%" + textBox2.Text + "%' AND ISNULL(MF003,'')='" + gridView1.GetFocusedRowCellValue("工序号").ToString().Trim() + "' ORDER BY MF003,MG006";
                    }
                }
                else
                {
                    if (checkBox1.CheckState == CheckState.Checked)
                    {
                        strSql = "SELECT RIGHT('0000'+RTRIM(CAST(ROW_NUMBER()OVER(ORDER BY MF003,MG003) AS CHAR(4))),4),B.TA001,B.TA002,MF003 AS TA003,ISNULL(MG006,'') MG006,TA034,C.MW002,MF008 AS MG007,CAST(TA017*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402) AS REAL) TA015,ISNULL(CEILING(YPSL),0),CAST(TA017*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402)-ISNULL(CEILING(YPSL),0) AS REAL) WBSL,0 HGSL,0 BLSL,0 GFSL,0 LFSL,'' YGBH,'' YGXM,'' ZRGX,'' BZ,TA001 AS TE001,TA002 AS TE002,CAST(ISNULL(MGD09,0) AS REAL) AS JJDJ,ISNULL(MF023,'') 下阶品号,ISNULL(F.MB002,'') 下阶品名,ISNULL(F.MB029,'') 下阶图号,CONVERT(CHAR(10),DATEADD(DAY,CEILING(ISNULL(MF009,0)/8),GETDATE()),111) 要求完工日期 FROM MOCTA B LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN BOMMG ON MF001=MG001 AND MF002=MG002 AND MF003=MG003 LEFT JOIN CMSMW C ON C.MW001=MF004 LEFT JOIN CMSMW D ON D.MW001=MGD01 LEFT JOIN V_SFCTE ON B.TA001=TE006 AND B.TA002=TE007 AND MF003=TE008 LEFT JOIN ITEM E ON E.ITEM_CODE=MF023 LEFT JOIN INVMB F ON MF023=F.MB001 WHERE  B.TA001='" + gridView1.GetFocusedRowCellValue("单别").ToString() + "' AND B.TA002='" + gridView1.GetFocusedRowCellValue("单号").ToString() + "' AND ISNULL(D.MW002,'') LIKE '%" + textBox2.Text + "%' ORDER BY MF003,MG006";
                    }
                    else
                    {
                        //MessageBox.Show("1", "ts");
                        strSql = "SELECT RIGHT('0000'+RTRIM(CAST(ROW_NUMBER()OVER(ORDER BY MF003,MG003) AS CHAR(4))),4),B.TA001,B.TA002,MF003 AS TA003,ISNULL(MG006,'') MG006,TA034,C.MW002,MF008 AS MG007,CAST(TA017*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402) AS REAL) TA015,ISNULL(CEILING(YBSL),0),CAST(TA017*ISNULL((SELECT TOP 1 R_QTY_PER FROM d_Bom('" + gridView1.GetFocusedRowCellValue("品号").ToString() + "') WHERE RTRIM(R_YJPH)=RTRIM(MF023)),1)*(1+INVMB.MB402)-ISNULL(CEILING(YBSL),0) AS REAL) WBSL,0 HGSL,0 BLSL,0 GFSL,0 LFSL,'' YGBH,'' YGXM,'' ZRGX,'' BZ,TA001 AS TE001,TA002 AS TE002,CAST(ISNULL(MGD09,0) AS REAL) AS JJDJ,ISNULL(MF023,'') 下阶品号,ISNULL(F.MB002,'') 下阶品名,ISNULL(F.MB029,'') 下阶图号,CONVERT(CHAR(10),DATEADD(DAY,CEILING(ISNULL(MF009,0)/8),GETDATE()),111) 要求完工日期 FROM MOCTA B LEFT JOIN INVMB ON TA006=MB001 LEFT JOIN BOMMF ON MB010=MF001 AND MB011=MF002 LEFT JOIN BOMMG ON MF001=MG001 AND MF002=MG002 AND MF003=MG003 LEFT JOIN CMSMW C ON C.MW001=MF004 LEFT JOIN CMSMW D ON D.MW001=MGD01 LEFT JOIN V_SFCTE ON B.TA001=TE006 AND B.TA002=TE007 AND MF003=TE008 LEFT JOIN ITEM E ON E.ITEM_CODE=MF023 LEFT JOIN INVMB F ON MF023=F.MB001 WHERE  B.TA001='" + gridView1.GetFocusedRowCellValue("单别").ToString() + "' AND B.TA002='" + gridView1.GetFocusedRowCellValue("单号").ToString() + "' AND ISNULL(D.MW002,'') LIKE '%" + textBox2.Text + "%' ORDER BY MF003,MG006";
                    }
                }
                DataTable dt;
               
                //textBox12.Text = strSql;
                // MessageBox.Show("1", "TS");
                //return;
                db = new DataBase();
                dt = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {

                    int index = this.dataGridView2.Rows.Add();
                    //MessageBox.Show(index.ToString(), "1");
                    this.dataGridView2.Rows[index].Cells[0].Value = false;
                    String XH = "000" + (index + 1).ToString();
                    XH = XH.Substring(XH.Length - 4, 4);
                    this.dataGridView2.Rows[index].Cells[1].Value = XH;
                    this.dataGridView2.Rows[index].Cells[2].Value = dt.Rows[i][1].ToString();
                    this.dataGridView2.Rows[index].Cells[3].Value = dt.Rows[i][2].ToString();
                    this.dataGridView2.Rows[index].Cells[4].Value = dt.Rows[i][3].ToString();
                    this.dataGridView2.Rows[index].Cells[5].Value = dt.Rows[i][4].ToString();
                    this.dataGridView2.Rows[index].Cells[6].Value = dt.Rows[i][5].ToString();
                    this.dataGridView2.Rows[index].Cells[7].Value = dt.Rows[i][6].ToString();
                    this.dataGridView2.Rows[index].Cells[8].Value = dt.Rows[i][7].ToString();
                    this.dataGridView2.Rows[index].Cells[9].Value = dt.Rows[i][8].ToString();
                    this.dataGridView2.Rows[index].Cells[10].Value = dt.Rows[i][9].ToString();
                    this.dataGridView2.Rows[index].Cells[11].Value = dt.Rows[i][10].ToString();
                    this.dataGridView2.Rows[index].Cells[12].Value = dt.Rows[i][11].ToString();
                    this.dataGridView2.Rows[index].Cells[13].Value = dt.Rows[i][12].ToString();
                    this.dataGridView2.Rows[index].Cells[14].Value = dt.Rows[i][13].ToString();
                    this.dataGridView2.Rows[index].Cells[15].Value = dt.Rows[i][14].ToString();
                    this.dataGridView2.Rows[index].Cells[16].Value = dt.Rows[i][15].ToString();
                    this.dataGridView2.Rows[index].Cells[17].Value = dt.Rows[i][16].ToString();
                    this.dataGridView2.Rows[index].Cells[18].Value = dt.Rows[i][17].ToString();
                    this.dataGridView2.Rows[index].Cells[19].Value = dt.Rows[i][18].ToString();
                    this.dataGridView2.Rows[index].Cells[20].Value = dt.Rows[i][19].ToString();
                    this.dataGridView2.Rows[index].Cells[21].Value = dt.Rows[i][20].ToString();
                    this.dataGridView2.Rows[index].Cells[22].Value = dt.Rows[i][21].ToString();
                    this.dataGridView2.Rows[index].Cells[23].Value = dt.Rows[i][22].ToString();
                    this.dataGridView2.Rows[index].Cells[24].Value = dt.Rows[i][23].ToString();
                    this.dataGridView2.Rows[index].Cells[25].Value = dt.Rows[i][24].ToString();
                    this.dataGridView2.Rows[index].Cells[26].Value = dt.Rows[i][25].ToString();
                }

            }
            catch
            {

            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            int[] rows = this.gridView1.GetSelectedRows();
            foreach (int row in rows)
            {
                //开始生成单据
                String strSql = "";
                String code = "";
                string date = dateTimePicker1.Value.ToShortDateString();
                String mo_db = this.gridView1.GetRowCellValue(row, "单别").ToString();
                String mo_dh = this.gridView1.GetRowCellValue(row, "单号").ToString();
               strSql = "SELECT ISNULL(CAST(CAST(MAX(TD002) AS NUMERIC(11))+1 AS CHAR(11)) ,CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112)+'001') AS DH FROM SFCTD WHERE TD001='D420' AND TD002 LIKE CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112) + '%'";
                try
                {
                    sdr1 = db.GetDataReader1(strSql);

                    while (sdr1.Read())
                    {
                        code = sdr1["DH"].ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    //throw ex;
                }
                finally
                {
                    sdr1.Close();
                }

                 strSql = "INSERT INTO SFCTD(CREATOR,FLAG,TD001,TD002,TD003,TD004,TD005,TD006,TD007,TD008,TD009,TD010,TD011,TD012,TD013,TD014,TD015,TD016,TD017,TD018,TD019,TD020,UDF01)  SELECT '" + PropertyClass.OperatorCode + "',1,'D420','" + code + "',CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112),'" + textBox1.Text + "','Y','',0,CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112),'','N',0,'','','',0,0,0,'**********',0,'********************','白班'";
                //textBox12.Text = strSql;
                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {
                    }
                    else
                    {
                        MessageBox.Show("生成报工单头失败，请检查" + code, "软件提示");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
                //开始产生单身
                strSql = "INSERT INTO SFCTE(CREATE_DATE,FLAG,TE001,TE002,TE003,TE004,TE005,TE006,TE007,TE008,TE009,TE010,TE011,TE012,TE013,TE014,TE015,TE016,TE017,TE018,TE019,TE020,TE021,TE022,TE023,TE024,TE025,TE026,TE027,TE028,TE029,TE030,TE031,TE032,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF07,UDF08,UDF09,UDF10,UDF11,UDF12,UDF57,UDF58,UDF59,UDF60) SELECT REPLACE(REPLACE(REPLACE(CONVERT(CHAR(20),GETDATE(),120),'-',''),':',''),' ',''),1,TD001,TD002,RIGHT('0000'+RTRIM(CAST(ROW_NUMBER()OVER(ORDER BY MF003) AS CHAR(4))),4),'','',TA001,TA002,A.MF003,A.MF004,'1',0,0,0,'Y','',0,B.TA006,B.TA034,B.TA035,MB004,'','',CONVERT(CHAR(8),DATEADD(DAY,0,'" + date + "'),112),'',0,0,0,0,0,'1',0,'********************',0,0,";
                strSql = strSql + "0,0,0,0,MF008,'','','" + code + "','" + code + "',MF008,'','','','','','',B.TA015,0,0,0 FROM SFCTD LEFT JOIN SFCTE ON TD001=TE001 AND TD002=TE002,MOCTA B LEFT JOIN INVMB ON B.TA006=MB001 LEFT JOIN BOMMF A ON MB010=MF001 AND MB011=MF002 WHERE TD001='D420' AND TD002='" + code + "' AND B.TA001='" + mo_db + "' AND B.TA002='" + mo_dh + "' GROUP BY TA001,TA002,TA003,TD001,TD002,A.MF003,A.MF004,A.MF008,B.TA006,B.TA015,B.TA034,B.TA035,MB004";
                textBox12.Text = strSql;
                MessageBox.Show("111", "ts");
                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {
                    }
                    else
                    {
                        MessageBox.Show("生成报工单头失败，请检查" + code, "软件提示");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }

                string x1 = "D420";
                string x2 = code;
                string x3 = "";
                SJCD xx = new SJCD(x1, x2, x3);
                LabelPrints L = new LabelPrints();
                L.cdssh = new LabelPrints.Cdssh(textBox2_TextChanged);
                L.Show();
            }
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.CheckState == CheckState.Unchecked)
            {
                button2.Text = "派工";
                dataGridView2.Columns["YJCL"].HeaderText = "可派数量";
                dataGridView2.Columns["YBSL"].HeaderText = "已派数量";
                dataGridView2.Columns["WBSL"].HeaderText = "未派数量";
                dataGridView2.Columns["HGSL"].HeaderText = "派工数量";
                dataGridView2.Columns["GFSL"].Visible = false;
                dataGridView2.Columns["LFSL"].Visible = false;
                dataGridView2.Columns["BLSL"].Visible = false;
                checkBox1.CheckState = CheckState.Checked;
            }
            else
            {
                button2.Text = "报工";
                dataGridView2.Columns["YJCL"].HeaderText = "可报数量";
                dataGridView2.Columns["YBSL"].HeaderText = "已报数量";
                dataGridView2.Columns["WBSL"].HeaderText = "未报数量";
                dataGridView2.Columns["HGSL"].HeaderText = "合格数量";
                dataGridView2.Columns["GFSL"].Visible = true;
                dataGridView2.Columns["LFSL"].Visible = true;
                dataGridView2.Columns["BLSL"].Visible = true;
                checkBox1.CheckState = CheckState.Unchecked;
            }
        }

        private void gridView1_MasterRowGetLevelDefaultView(object sender, MasterRowGetLevelDefaultViewEventArgs e)
        {
            //MessageBox.Show("123","系统提示");
           // e.DefaultView = gridView2;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                gridControl2.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void dataGridView2_CurrentCellChanged(object sender, EventArgs e)
        {
            
        }

        private void dataGridView2_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

        }

        private void button17_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                gridControl4.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            string strSql = "";
            strSql = "SELECT MO.DOC_NO 工单号,ITEM.ITEM_NAME 品名,ITEM.ITEM_SPECIFICATION 规格,CAST(MO.PLAN_QTY AS REAL) 预计产量,(SELECT CAST(ISNULL(MIN(YPSL),0) AS REAL) FROM V_SFCTE WHERE TE006+'-'+TE007=MO.DOC_NO) 已派数量,(SELECT CAST(ISNULL(MIN(YBSL),0) AS REAL) FROM V_SFCTE WHERE TE006+'-'+TE007=MO.DOC_NO) 已报数量,CAST(MO.REQ_QTY AS REAL) 送检数量,CAST(MO.COMPLETED_QTY AS REAL) 入库数量  FROM MO LEFT JOIN ITEM ON MO.ITEM_ID=ITEM.ITEM_BUSINESS_ID WHERE ACTUAL_COMPLETE_DATE>='" + dateTimePicker14.Value.ToShortDateString() + "' AND ACTUAL_COMPLETE_DATE<='" + dateTimePicker15.Value.ToShortDateString() + "'";
            //textBox18.Text = strSql;
            try
            {
                DataTable dt;
                dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                this.gridControl4.DataSource = dt;
                gridView4.OptionsView.EnableAppearanceEvenRow = true;
                gridView4.OptionsView.EnableAppearanceOddRow = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void gridView1_FocusedRowObjectChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowObjectChangedEventArgs e)
        {

        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
